一、转发参数:
1.将jsp里面的参数通过LoginServlet转到PageSelvert:
@WebServlet(“/login”)publicclassLoginServletextendsHttpServlet{protectedvoiddopost(HttpServletRequest request,HttpServletResponse response)throwsServletException,IOException{//转发到PageServlet去Request.getRequestDispatcher(“/page”),forword(request,reapomse); }}@WebServlet(“page”)publicclassPageServletextendsHttpServlet{protectedvoiddopost(HttpServletRequest request,HttpServletResponse response)throwsServletException,IOException{ String usename = request.getParameter(“usename”);String password = request.getParameter(“password”);System.out.pritln(usename);System.out.pritln(passworrd);}}
2.将LoginServlet里面的存放的值带给PageServlet中去:
@WebServlet(“/login”)PublicclassLoginServletextendsHttpServlet{Protectedvoiddopost(HttpServletRequest request,HttpServletResponse response){//在request对象里,设置属性值,只要是同一个request对象才能获得此数据(所以转发可以,重定向不行,重定向是两个)//将email带过去(只能在前面,如是在传的后(getRequestDisPatcher后面),则得不到)Request.setAttribute(“email”,”[email protected]”);Request.getRequestDisPatcher(“/page”),forword(request,response);}}@WebServlet(“/page”)PublicclassPageServletextendsHttpServlet{Protectedvoiddopost(HttpServletRequest request,HttpServletResponse response){//得到[email protected] 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
<--欢迎页面是转发机制的跳转-->index.jsp
目的:隐藏jsp 将访问页面改成如下:
/WEB-INF/pages/index.jsp
前面加“/”直接到以其为根目录的地方
//admin为一个虚拟夹子@WebServlet("/admin/test")publicclassTestServletentendsHttpServlet{protectedvoiddeGet(HttpServletQuest req,HttpServletResponse resp)throwsServletException,IOEception{resp.sendRedirect("index.jsp");}}
想访问到的3种方法:
//”..”代表在index.jsp目录下向上跳一个目录resp.sendRedirect("../index.jsp");
System.out.println(req.getContextPath());//servlet7_urlresp.sendRedirect(req.getContextPath()+"/index.jsp");
resp.sendRedirect(req.getContextPath() +"/");
三、乱码问题:
Tomcat7 版本转换乱码需要看方法来转get string类转码 post就直接设置编码就可以了
String s=req.getParament("text"); ↓
System.out.println(newString(s.getBytes(ISO-8859),"utf-8"));req,setCharacterEncoding("UTF-8");String s=req.getParamenter("test"); ↓
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://tomcat8Request.setCharacterEncoding(“UTF-8”);//tomcat 7String s=newString(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);