转发.隐藏JSP.URL地址.md

一、转发参数:

1.将jsp里面的参数通过LoginServlet转到PageSelvert:

@WebServlet(“/login”) public class LoginServlet extends HttpServlet{ protected void dopost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{ //转发到PageServlet去 Request.getRequestDispatcher(“/page”),forword(request,reapomse);
}} @WebServlet(“page”) public class PageServlet extends HttpServlet{ protected void dopost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{ 

String usename = request.getParameter(“usename”);
String password = request.getParameter(“password”);
System.out.pritln(usename);
System.out.pritln(passworrd);
}
}

2.将LoginServlet里面的存放的值带给PageServlet中去:

@WebServlet(“/login”)
Public class LoginServlet extends HttpServlet{ Protected void dopost(HttpServletRequest  request,HttpServletResponse  response){ //在request对象里,设置属性值,只要是同一个request对象才能获得此数据(所以转发可以,重定向不行,重定向是两个) //将email带过去(只能在前面,如是在传的后(getRequestDisPatcher后面),则得不到) Request.setAttribute(“email”,”123456@163.com”);
Request.getRequestDisPatcher(“/page”),forword(request,response);
}
} @WebServlet(“/page”)
Public class PageServlet extends HttpServlet{ Protected void dopost(HttpServletRequest  request,HttpServletResponse  response){ //得到[email protected] String email = (String)request.getAttribute(“email”); //删除email值 Request.removeAttribute(“email”); //拿到所有的名字 Request.getAttributeNames();
String usename = request.getParameter(“usename”);
String password = request.getParameter(“password”);
}
}

** 转发参数:** removeAttribute 删除 getAttributeNames 拿到所有的名字 setAttribute 设置值 getAttribute 得到值

request response 他们的生命周期,就在请求和响应之间结束。

二、隐藏JSP:

可以将JSP放入WEB-INF目录,以后只能用转发来访问以下的JSP

<welcome-file-list> <-- 欢迎页面是转发机制的跳转 --> <welcome-file>index.jsp</welcome-file> </welcome-file-list>

目的:隐藏jsp 将访问页面改成如下:

<welcome-file-list> <welcome-file>/WEB-INF/pages/index.jsp</welcome-file> </welcome-file-list>

前面加“/”直接到以其为根目录的地方

//admin为一个虚拟夹子 @WebServlet("/admin/test") public class TestServlet entends HttpServlet{ protected void deGet(HttpServletQuest req,HttpServletResponse resp)throws ServletException,IOEception{
resp.sendRedirect("index.jsp");
}
}

想访问到的3种方法:

//”..”代表在index.jsp目录下向上跳一个目录 resp.sendRedirect("../index.jsp");
System.out.println(req.getContextPath());//servlet7_url resp.sendRedirect(req.getContextPath()+"/index.jsp");
resp.sendRedirect(req.getContextPath() + "/");

三、乱码问题:

Tomcat7 版本转换乱码需要看方法来转get string类转码 post就直接设置编码就可以了

String s=req.getParament("text");
        ↓
<from actoin="lm" method="get">
System.out.println(new String(s.getBytes(ISO-8859),"utf-8"));
req,setCharacterEncoding("UTF-8");
String s=req.getParamenter("test");
        ↓
<from actoin="lm" method="post">
System.out.println(s);

Tomcat8 版本就不需要半段方法,直接设置转码就可

req.setCharacterEncoding("UTF-8");
String s=req.getParameter("test");
System.out.println(s);

如果不转码,直接打印,就会出现乱码,如下图:

如果想将一个Servlet里面的字符传到另一个Servlet中去,需要进行转码,如:

Request.sendRedirect(text1?name=”狗子”);

此时应写成:

Text0Servlet:
Request.sendRedirect(“text1?name=”+URLEncode.encode(“狗子”));
Text1Servlet: //tomcat8 Request.setCharacterEncoding(“UTF-8”); //tomcat 7 String  s=new String(request.getParameter(“name”).getBytes
(“ISO-8859-1”),”utf-8”);
System.out.println(request.getParameter(“name”));
resp.sendRedirect("test1?name="+URLEncoder.encode("多态","UTF-8"));
                           ↓
req.setCharacterEncoding("UTF-8"); //String s=new String(req.getParameter("name").getBytes("ISO-8859-1"),"UTF-8") System.out.println(req.getParameter);
时间: 2024-08-24 16:03:21

转发.隐藏JSP.URL地址.md的相关文章

jsp传递url地址里带有&amp;的无法接收的解决方法

当需要传递url地址的时候,比如修改带有分页的帖子后.需要传递页码当修改完毕后可以跳回修改的页面.当有&这个符号时.request只能取得不到这个符号,和这个符号的内容. 比如传递的url地址 http://localhost/newbbs/update.jsp?id=32&urlname=http://localhost/newbbs/articledetailflat.jsp?rootid=30&pageno=2 通过request.getParmeter("urln

求类似javaniu的url地址伪静态重写

原文:求类似javaniu的url地址伪静态重写 代码下载地址:http://www.zuidaima.com/share/1550463243504640.htm 就是将url为.jsp或者.action结尾的地址,弄成像javaniu这样.htm的,把地址隐藏起来,不知道如何做,求一个demo学习一下,自己弄了几天了,貌似没什么好的成效...看看大家有没有什么好方法呢?urlrewrite好像每个页面都要配置from to 求类似javaniu的url地址伪静态重写

python-获得一个URL地址的扩展名

获得一个URL地址的扩展名 如:http://www.cnblogs.com/fnng/archive/2013/05/20/3089816.html  的扩展名为html对于这个问题同样使用正则式来解决 import re def strings(url): listt = ['.php','.html','.asp','.jsp'] for lis in listt: suffix = re.findall(lis,url) //re.findall(pattern, string[, fl

web工程中URL地址的推荐写法

在Javaweb开发中,只要写URL地址,建议以“/”开头,也就是使用绝对路径的方式. “/”:如果是给服务器的,代表当前的web工程.给浏览器的,代表webapps目录 代表web工程 ①.ServletContext.getRealPath(String path)获取资源的绝对路径 ②.在服务器端forward到其他页面 1 /** 2 * 2.forward 3 * 客户端请求某个web资源,服务器跳转到另外一个web资源,这个forward也是给服务器用的, 4 * 那么这个"/&qu

Javascript验证用户输入URL地址是否正确

<script type="text/javascript">function checkUrl() { var url = document.getElementById('url').value; if (url==''){ alert('URL 地址不能为空'); return false; } else if (!isURL(url)) { alert('URL 的格式应该是 http://www.111cn.net'); return false; } else

获取网页URL地址及参数等的两种方法(js和C#)

转:获取网页URL地址及参数等的两种方法(js和C#) 一 js 先看一个示例 用javascript获取url网址信息 <script type="text/javascript"> document.write("location.host="+location.host+"<br>"); document.write("location.hostname="+location.hostname+&

转载:web工程中URL地址的推荐写法

在JavaWeb开发中,只要是写URL地址,那么建议最好以"/"开头,也就是使用绝对路径的方式,那么这个"/"到底代表什么呢?可以用如下的方式来记忆"/":如果"/"是给服务器用的,则代表当前的web工程,如果"/"是给浏览器用的,则代表webapps目录. 1."/"代表当前web工程的常见应用场景 ①.ServletContext.getRealPath(String path)获取

nginx 配置web 虚拟目录 并且codeIgniter,thinkphp 重新url 地址

nginx 配置虚拟目录并且url 重写 server { #侦听80端口 listen 8090; #定义使用www.xx.com访问 server_name 127.0.0.1; #设定本虚拟主机的访问日志 access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; root /home/lxy/www/cs/; #定义服务器的默认网站根目录位置 #默认请求 location / { index inde

JAVA 取得当前目录的路径/Servlet/class/文件路径/web路径/url地址

JAVA 取得当前目录的路径/Servlet/class/文件路径/web路径/url地址 在写java程序时不可避免要获取文件的路径...总结一下,遗漏的随时补上1.可以在servlet的init方法里String path = getServletContext().getRealPath("/");这将获取web项目的全路径例如 :E:\eclipseM9\workspace\tree\tree是我web项目的根目录 2.你也可以随时在任意的class里调用this.getCla