Analysis of the difference between JSP forwarding and redirection

  • 2020-05-30 20:56:08
  • OfStack

For a long time not to see, and the basis just to forget. Have a good look again today. Ha ha ha... Review the old and learn the new!!

1. RequestDispatcher. forward () � � forward
It works on the server side, and when forward() is used, Servlet engine pass HTTP request from the current Servlet or JSP to another Servlet,JSP or normal HTML file, that is, your form submit to a.jsp, forward() redirect to b.jsp in a.jsp, all information submitted by form is available at b.jsp, and the parameters are automatically passed.
However, forward() cannot be redirected to an jsp file with frame, it can be redirected to an html file with frame, and forward() cannot be passed with arguments later, such as servlet? name=frank, this does not work, can be within the program through response.setAttribute ("name", name) to the next page.

The browser address bar URL remains the same after forwarding.

Example: redirection in servlet

public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
         response.setContentType("text/html; charset=gb2312");
         ServletContext sc = getServletContext();
         RequestDispatcher rd = null;
         rd = sc.getRequestDispatcher("/index.jsp");      // Directed page 
         rd.forward(request, response);

Usually used in servlet, not jsp.

2. response. sendRedirect () � � redirection
sendRedirect() can be passed with parameters, such as servlet? name=frank to the next page, and it can be redirected to a different host. sendRedirect() can be redirected to jsp files with frame.
After the redirect, URL of the redirect page will appear in the browser's address bar
Example: redirect in servlet

public void doPost(HttpServletRequest request,HttpServletResponse response)
         throws ServletException,IOException
         response.setContentType("text/html; charset=gb2312");

Since response is an implicit object in the jsp page, you can use response.sendRedirect () to directly reposition the jsp page.

Since response is an implied object in the jsp page, response.sendRedirect () can be directly repositioned in the jsp page.

(1). When response.sendRedirect is used, there cannot be HTML output in front.
This is not absolute, no HTML output actually means no HTML can be sent to the browser. The fact that server now has an cache mechanism, 1 generally in 8K (I mean JSP SERVER), means that unless you turn off cache, or you use out.flush () to force the refresh, a small amount of HTML output is allowed before sendRedirect is used.
(2).response.sendRedirect should be followed by a sentence return;
We already know that response.sendRedirect does the steering through the browser, so there is no actual action until the page is processed. Now that you're doing the steering, what's the point of the output? It is also possible that the steering will fail due to the subsequent output.
(1).Request Dispatcher.forward() is the direction of control in the container, and the address after the direction is not displayed in the address bar of the client browser;
(2). response.sendRedirect () is a full jump, and the browser will get the jump's address and resend the request link. This way, you can see the link address after the jump in the browser's address bar.
The former is more efficient, and the RequestDispatcher.forward () method is used when the former meets the needs.

Note: in some cases, for example, if you need to jump to a resource on one of the other servers, you must use the HttpServletResponse.sendRequest () method.

3. < jsp:forward page="" / >

The bottom part of it is implemented by RequestDispatcher, so it bears the imprint of the RequestDispatcher.forward () method.

If the < jsp:forward > There is a lot of output before, the previous output has filled the buffer, will be automatically output to the client, then this statement will not work, this point should be noted.
Also note: it does not change the browser address; a refresh will result in a repeat commit

4. Modify the Location property of HTTP header to redirect
Redirects the page by setting the direct modification address bar.
The code of jsp is as follows:

 The < %
String newLocn = "/newpath/jsa.jsp";
% > 

5.JSP is now automatically redirected to another page after a certain number of seconds
In the html file, the following code:
< meta http-equiv="refresh" content="300; url=target.jsp" >
What it means: after 5 minutes the page you are browsing will automatically change to target.html. In the code, 300 is the delay time of the refresh, measured in seconds. targer.html is the target page you want to turn to. If it is this page, it will automatically refresh this page.
As can be seen from the above, setHeader can be used to automatically redirect a page to another page after a certain number of seconds.
Key codes:

           String content=stayTime+";URL="+URL;

Related articles: