Redirects the aggregate in Jsp Servlet

  • 2020-05-09 19:03:13
  • OfStack

1. RequestDispatcher.forward()
It works on the server side, When using forward(),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() redirects to b.jsp in a.jsp, all information submitted by form is available at b.jsp, and the parameters are automatically passed. 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 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 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 ()
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 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 cannot be output before;
This is not absolute, and the fact that you can't have HTML output means you can't have HTML sent to the browser. The fact that server now has an cache mechanism, 1 generally in 8K (I mean JSPSERVER), 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 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.
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.
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:
< %
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, 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;
response.setHeader("REFRESH",content);


Related articles: