1.RequestDispatcher.forward()
works on the server side. When using forward(), the Servlet engine passes the HTTP request from the current Servlet or JSP to another Servlet, JSP or ordinary HTML file, that is, your form is submitted to a.jsp, forward() is used in a.jsp to redirect to b.jsp. At this time, all the information submitted by the form can be obtained in b.jsp, and the parameters are automatically passed.
However, forward() cannot redirect to a jsp file with a frame. It can redirect to an html file with a frame. At the same time, forward() cannot be passed with parameters at the end, such as servlet?name=frank. If this does not work, you can pass the response in the program. .setAttribute("name",name) to pass to the next page.
The browser address bar URL remains unchanged after redirection.
Example: Redirect in servlet
Code content is usually used in servlets, not jsp.
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);
}
2.response.sendRedirect()
It works on the user's browser. sendRedirect() can pass parameters, such as servlet?name=frank, to the next page. At the same time, it can redirect to different hosts. sendRedirect() can redirect frames. jsp file.
After redirection, the URL of the redirected page will appear on the browser address bar.
Example: Redirect in servlet
Code content
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, response.sendRedirect() can be used to directly implement relocation in the jsp page.
Note:
(1). When using response.sendRedirect, there cannot be HTML output in front.
This is not absolute. No HTML output actually means that no HTML can be sent to the browser. In fact, today's servers have a cache mechanism, usually 8K (I mean JSP SERVER). This means that unless you turn off the cache, or you use out.flush() to force a refresh, then before using sendRedirect, A small amount of HTML output is also allowed.
(2) After .response.sendRedirect, it should be followed by a return;
we already know that response.sendRedirect is redirected through the browser, so there will be no actual action until the page processing is completed. Now that you have already done the steering, what is the significance of the final output? And it is possible that the steering fails due to the subsequent output.
Compare:
(1).Request Dispatcher.forward() is the redirection of control in the container, and the redirected address will not be displayed in the address bar of the client browser;
(2).response.sendRedirect() is a complete jump. The browser will get the jump address and resend the request link. In this way, the link address after the jump can be seen in the browser's address bar.
The former is more efficient. When the former can meet the needs, try to use the RequestDispatcher.forward() method.
Note: In some cases, for example, if you need to jump to a resource on another server, you must use the HttpServletResponse.sendRequest() method.
3.<jsp:forward page="" />
The underlying part is implemented by RequestDispatcher, so it bears the imprint of the RequestDispatcher.forward() method. If there is a lot of output before <jsp:forward>, and the previous output has made the buffer full and will be automatically output to the client, then this statement will not work. This should be paid special attention to.
Also note: it cannot change the browser address. Refreshing will result in repeated submissions.
4. Modify the Location attribute of the HTTP header to redirect.
Redirect the page by directly modifying the address bar.
The jsp file code is as follows:
Code content: 5. JSP implements automatic redirection to another page after staying on a certain page for 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 the target.html page. 300 in the code is the refresh delay time in seconds. target.html is the target page you want to redirect to. If it is this page, it will automatically refresh this page.
As can be seen from the above, you can use setHeader to automatically redirect to another page after staying on a page for a few seconds.
Key code:
Code content
<%
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
String newLocn = "/newpath/jsa.jsp";
response.setHeader("Location",newLocn);
%>
String content=stayTime+";URL="+URL;
response.setHeader("REFRESH",content);