Illustrate the difference between redirection and request forwarding

Source: Internet
Author: User

redirect

  The sendredirect (java.lang.String location) method of the HttpServletResponse object is called a redirect .

If the location address is preceded by "/", the request is relative to the root of the servlet container, such as http://localhost:8080, and if the location address does not precede the "/", it represents the URI of the current request to find the address.

Request Forwarding

  RequestDispatcher :forward (ServletRequest request, servletresponse Response) method is called forwarding.

Experiment Example 1: Redirect and request forwarding seem to cause page jumps

  first page of first.jsp:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

  The second page is a servlet:

To forward with a request:

Package Com.shengqishiwind.servlet;import Java.io.ioexception;import Javax.servlet.requestdispatcher;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class Second extends httpservlet{public    void doget (HttpServletRequest request, httpservletresponse response)            throws Servletexception, IOException    {        process (request, response);    }    public void DoPost (HttpServletRequest request, httpservletresponse response)            throws Servletexception, IOException    {        process (request, response);    }    private void process (HttpServletRequest request,            HttpServletResponse response) throws Servletexception, IOException    {        //Request forward        RequestDispatcher rd = Request.getrequestdispatcher ("third.jsp");        Rd.forward (request, response);}    }

With redirection, the processing method is changed to:

    private void process (HttpServletRequest request,            HttpServletResponse response) throws Servletexception, IOException    {        //redirect        Response.sendredirect ("third.jsp");    }

  The third page is third.jsp

 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

No matter how the request is forwarded or redirected, the first page clicks Submit, and it goes to the third page:

But actually the actual operation is still very different, see the following example.

Experiment Example 2: If you want to get the user name entered on the first page in the third page

  The implementation of request forwarding is relatively simple, the second page of the servlet code without adding anything, you can directly from the third page getparameter, that is, the body of the third.jsp changed to:

  <body> This is    my third page. <br>        User name: <%=request.getparameter ("username")%>      </ Body>

Then enter shengqishiwind on the first page, after submission, the third page displays:

  How should redirects be implemented get the user name of the first page submission?

The third page uses the above code (through GetParameter to get the parameters), the second page to redirect the jump mode, once again, you can see the display:

Try the second page again (store the value via SetAttribute):

    private void process (HttpServletRequest request,            HttpServletResponse response) throws Servletexception, IOException    {        String username = request.getparameter ("username");        REDIRECT        Request.setattribute ("username", username);        Response.sendredirect ("third.jsp");    }

Then the third page gets:

     User name: <%=request.getattribute ("username")%>    

Still no, the resulting value is still null.

the difference between redirection and request forwarding

Request Forwarding:

RequestDispatcher is obtained by invoking the Getrequestdispatcher () method of the HttpServletRequest object, which is the method that belongs to the request object.

  request forwarding, the entire process is in the same request.

Regardless of the number of components experienced, you can get the desired value directly from the request.

When using request forwarding, the URL to the results page is: http://localhost:8080/HelloWeb/Second?username=wind

REDIRECT :

Sendredirect () is the method of the HttpServletResponse object, which is the method of the Response object.

Now that the method of the response object is called, it indicates that the entire request process is over, and the server starts returning the execution results to the client.

A response is returned to the client after the Sendredirect call, which tells the client which page to turn to, and then the client sends a new request to the target page.

That is, in the process of redirection, the client actually sends two requests to the server.

When using redirection, the URL of the result page is: http://localhost:8080/HelloWeb/third.jsp

Indicates that the client has already known the address of the resulting page and is the new request to resend.

redirect Mode in Firebug diagram:

  Summary Memory : Request forwarding only one request, so go ahead, call the method called Forward, and redirect requires the client to resend the request, the call is Sendredirect, the name has a re, indicating repetition.

Illustrate the difference between redirection and request forwarding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.