I. RequestDispatcher. forward ()
It works on the server. When forward () is used, the Servlet engine transmits the HTTP request from the current Servlet or jsp to another Servlet JSP or common HTML file.
Parameters can be passed as follows:
RequestDispatcher rd = context. getRequestDispatcher ("/jsp/forward. jsp? Name = zhangsan ");
RequestDispatcher rd = context. getRequestDispatcher ("/F? Name = jingjing ");
The URL in the browser address bar remains unchanged after redirection
<P> example: redirect in servlet </p> <pre class = "java" name = "code"> public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Response. setContentType ("text/html ");
ServletContext context = getServletContext ();
// Forward () to jsp
RequestDispatcher rd = context. getRequestDispatcher ("/jsp/forward. jsp? Name = zhangsan ");
// Forward () can be passed to the url-pattern servlet that makes servlet F servlet
// RequestDispatcher rd = context. getRequestDispatcher ("/F? Name = jingjing ");
Request. setAttribute ("info", "hello"); // PASS Parameters
Rd. forward (request, response );
}
The project name must contain "/", "/" before the path. Otherwise, the following exception is reported:
Java. lang. IllegalArgumentException: Path F does not start with a "/" character
Com.org. MyServlet. doGet (MyServlet. java: 31)
Javax. servlet. http. HttpServlet. service (HttpServlet. java: 617)
Javax. servlet. http. HttpServlet. service (HttpServlet. java: 717)
The following parameters are obtained in forward. jsp:
String name = request. getParameter ("name ");
The parameter obtained in servlet is:
<Pre class = "java" name = "code"> String name = request. getParameter ("name ");
String info = (String) request. getAttribute ("info ");
Ii. response. sendRedirect ()
It works on the user's browser. sendRedirect () can be passed with parameters, such as servlet? Name = zhangsan
After redirection, the URL of the redirection page will appear in the address bar of the browser.
Example: Redirect jsp
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Response. setContentType ("text/html ");
Response. sendRedirect ("jsp/forward. jsp ");
// Parameter passing
// Response. sendRedirect ("jsp/forward. jsp? Name = zhangsan ");
Redirect servlet
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Response. setContentType ("text/html ");
Response. sendRedirect ("F ");
// Optional parameters
// Response. sendRedirect ("F? Name = zhagnsan ");
}
Comparison:
A) both of them can achieve page Jump. forward is the control direction in the container, and the redirection address is not displayed in the address bar of the client browser.
Response. sendRedirect () is a complete jump. the browser will get the jump address and resend the request link. In this way, you can see the link address after the jump from the address bar of the browser.
B) both of them can jump to jsp or servlet and both can pass parameters.
However, after the response. sendRedirect () jump, the code after the jump will not be executed.
Example:
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Response. setContentType ("text/html ");
PrintWriter out = response. getWriter ();
Out. println ("hello kity"); // No output
Response. sendRedirect ("F? Name = zhagnsan ");
Out. println ("good"); // No output
}
The former is more efficient. When the former can meet basic needs, use the RequestDispatcher. forward () method whenever possible.
Iii. Other jump Methods
A) modify the Location attribute of the HTTP header to redirect
B) implement page redirection by setting direct modification to the address bar
C) Set jsp to automatically redirect after several seconds
Response. setHeader ("refresh", "3; url = forward2.jsp ");
Note: Please note if there are any imperfections or errors.
From the itmyhome Column