Redirect the aggregate in Jsp Servlet

  • 2020-05-07 20:14:37
  • OfStack

1. RequestDispatcher.forward()
It works 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 submits to a.jsp. forward() is redirected to b.jsp in a.jsp. But 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, 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 ()
works on the user's browser. 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 the jsp file with frame.
After the redirect, URL of the redirect page appears 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 an implicit object in the jsp page, you can use response.sendRedirect () to directly reposition the jsp page.
Note:
(1) when response.sendRedirect is used, HTML output cannot be seen in front of it;
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 in 8K (I mean JSPSERVER), means that unless you turn cache off, or you use out.flush () to force a refresh, then a small amount of HTML output is allowed before sendRedirect.
(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've done the steering, what's the point of the output? And it is possible to fail the steering due to the output behind.
Comparison:
(1) Dispatcher.forward () is the steering of control in the container, and the address after steering will not be displayed in the address bar of the client browser;
(2) response.sendRedirect () is a full jump, the browser will get the jump address and resend the request link. This way, you can see the link address after the jump from the browser's address bar.
The former is more efficient, and RequestDispatcher.forward () is used whenever the former is needed.
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 underlying 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.
Also note: it cannot change the browser address, and a refresh will result in a repeat commit
4. Modify the Location property of HTTP header to redirect
Redirection of the page is achieved by setting the address bar to be modified directly.
The code of jsp is as follows:
< %
response. setStatus (HttpServletResponse. SC_MOVED_PERMANENTLY);
String newLocn = ". / newpath jsa jsp ";
response. setHeader (" Location newLocn);
% >
5. JSP now automatically redirects a page to another page after a few 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, in seconds. targer.html is the target page you want to turn to.
As can be seen from above, setHeader can be used to realize the automatic redirection to another page after a certain page stays for a few seconds.
Key codes:
String content=stayTime+";URL="+URL;
response.setHeader("REFRESH",content);


Related articles: