Forward, hide JSP, URL address

1. Forwarding parameters:

1. Transfer the parameters in jsp to PageSelvert through LoginServlet:

@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. Bring the stored value in LoginServlet to 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”);
}
}

** Forwarding parameters: ** removeAttribute delete getAttributeNames get all names setAttribute set value getAttribute get value

request response Their life cycle ends between request and response.

2. Hidden JSP:

You can put JSP into the WEB-INF directory, and you can only use forwarding to access the following JSPs in the future

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

20170707001

Purpose: Hide the jsp and change the access page to the following:

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

20170707001

20170707001

Add "/" in front of it directly to the place where it is the root directory

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

20170707001

3 ways to get access:

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

Third, the garbled problem:

To convert garbled characters in Tomcat7 version, you need to look at the method to convert the get string class to convert the post to set the encoding directly.

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);

The Tomcat8 version does not need the half-segment method, just set the transcoding directly

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

If you do not transcode and print directly, there will be garbled characters, as shown below:

20170707001

If you want to pass the characters in one servlet to another servlet, you need to transcode, such as:

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

This should be written as:

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);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326218907&siteId=291194637