Summary of several methods of page jump under jsp

  • 2020-05-19 05:30:48
  • OfStack

1. RequestDispatcher.forward()
On the server side, when forward() is used,Servlet engine passes the HTTP request from the current Servlet or JSP to another Servlet, JSP or normal HTML file, that is, your form is submitted to a.jsp, forward() is redirected to b.jsp in a.jsp, and all information submitted by form is in b.jsp But forward() cannot be redirected to an jsp file with frame, can be redirected to an html file with frame, and forward() cannot be passed with arguments, 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 the redirect.
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()
Working on the user's browser side,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 address bar.
Example: redirect in servlet
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
response. setContentType (" text/html; charset = gb2312 ");
response. sendRedirect (". / index jsp ");
}
Since response is the implied object of the jsp page, response.sendRedirect () can be directly repositioned in the jsp page.
Note:
(1) when using response.sendRedirect, HTML output is not allowed in front;
This is not absolute, no HTML output actually means no HTML can be sent to the browser. In fact, server now has the cache mechanism, 1 generally in 8K (I mean JSPSERVER), which means that unless you turn cache off, or you use out.flush () force 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.
Comparison:
(1) Dispatcher.forward() is the steering of control in the container, and the steering address will not be 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 RequestDispatcher.forward () is used whenever the former can be used.
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.
This statement will not work if there is a lot of output before and the previous output has filled the buffer and will be automatically output to the client, which should be noted.
Note: it does not change the browser address; a refresh will result in a repeat commit


4. Modify the HTTP header Location property to redirect
Redirects the page by setting the direct modification address bar.
The code of jsp is as follows:
The < %
response. setStatus (HttpServletResponse. SC_MOVED_PERMANENTLY);
String newLocn = ". / newpath jsa jsp ";
response. setHeader (" Location newLocn);
% >


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. Code:
String content = stayTime + "; URL = "+ URL;
response. setHeader (" REFRESH content);

The following are other netizens' articles
1. request. getRequestDispatcher (". / admin jsp "). forward (request response); // forward to admin.jsp

2. RequestDispatcher rd=request.getRequestDispatcher("/admin.jsp");
rd.forward(request, response);

3. getServletConfig().getServletContext().getRequestDispatcher("/admin.jsp").forward(request, response);

Other methods can, of course, use the html js method, which requires the output of jsp.


Related articles: