1. Requestdispatcher.forward ()
is on the server side, when using forward (), the Servlet engine passes HTTP requests from the current servlet or JSP to another servlet,jsp or normal HTML file, or your form commits to a.jsp, The forward () redirect to b.jsp is used in a.jsp, where all the information submitted by form is available in b.jsp and the parameters are automatically passed. However, forward () cannot be redirected to a JSP file with a frame, which can be redirected to an HTML file with a frame, while forward () cannot be passed back with parameters such as Servlet?name=frank, which is not possible. You can pass Response.setattribute ("name", name) to the next page within your program.
The browser address bar URL does not change after redirection.
Example: Redirecting in the 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"); //定向的页面
rd.forward(request, response);
}
Typically used in a servlet and not used in a JSP.
2. Response.sendredirect ()
is 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 different hosts, Sendredirect () can redirect the JSP file with frame.
The URL of the redirected page appears on the browser's address bar after redirection
Example: Redirecting in the servlet
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
response.setContentType("text/html; charset=gb2312");
response.sendRedirect("/index.jsp");
}
Because response is an implied object in JSP pages, you can use Response.sendredirect () directly in JSP pages to achieve relocation.
Attention:
(1) When using Response.sendredirect, the front cannot have the HTML output;
This is not absolute, and the inability to have HTML output actually means that HTML cannot be sent to the browser. In fact, the server now has the cache mechanism, generally in 8K (I mean JSP SERVER), which means that unless you close the cache, or you use the Out.flush () force refresh, then before using Sendredirect, A small amount of HTML output is also allowed.
(2) After Response.sendredirect, should be followed by a return.
We already know that Response.sendredirect is a browser to do the steering, so only after the page processing is complete, there will be actual action. Now that you've got to turn, what's the point of the output? And it is possible that the subsequent output will cause the steering failure.
Comparison:
(1) The Dispatcher.forward () is the steering of the control in the container, which will not show the turning address in the client browser address bar;
(2) Response.sendredirect () is a complete jump, the browser will get the address of the jump, and resend the request link. In this way, you can see the link address after the jump from the address bar of the browser.
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, you need to jump to a resource on a different server, you must use the Httpservletresponse.sendrequest () method.