Java Servlet (ix): The difference between a forwarding request and a redirect request

Source: Internet
Author: User

Forward:

<%Pagecontext.setattribute ("pagecontextattr", "Pagecontextattribute"); Request.setattribute ("requestattr", "Requestattribute"); Session.setattribute ("sessionattr", "Sessionattribute"); Application.setattribute ("applicationattr", "Applicationattribute"); %>PageContext:<%=Pagecontext.getattribute ("pagecontextattr")%><BR/>Request:<%=Request.getattribute ("requestattr")%><BR/>session:<%=Session.getattribute ("sessionattr")%><BR/>Application:<%=Application.getattribute ("applicationattr")%><BR/>    <%        StringServletname= "myelservletpage.jsp"; RequestDispatcher RequestDispatcher=Request.getrequestdispatcher ("/" +servletname);        Requestdispatcher.forward (request, response); // 1, redirect"/"represents the root directory of the current Web site//forward: "/"represents the root directory of the current web App. // 2, Forward: A resource that can only be forwarded to the current Web application;//REDIRECT : You can redirect to any resource, including external resources. %>

Redirect:

<%Pagecontext.setattribute ("pagecontextattr", "Pagecontextattribute"); Request.setattribute ("requestattr", "Requestattribute"); Session.setattribute ("sessionattr", "Sessionattribute"); Application.setattribute ("applicationattr", "Applicationattribute"); %>PageContext:<%=Pagecontext.getattribute ("pagecontextattr")%><BR/>Request:<%=Request.getattribute ("requestattr")%><BR/>session:<%=Session.getattribute ("sessionattr")%><BR/>Application:<%=Application.getattribute ("applicationattr")%><BR/>    <%        // 1, redirect"/"represents the root directory of the current Web site//forward: "/"represents the root directory of the current web App. Response.sendredirect ("myredirectpage.jsp"); // 2, Forward: A resource that can only be forwarded to the current Web application;//REDIRECT : You can redirect to any resource, including external resources. %>

Myelservletpage.jsp and myredirectpage.jsp content are the same:

PageContext:<%=Pagecontext.getattribute ("pagecontextattr") %><BR/>Request:<%=Request.getattribute ("requestattr") %><BR/>session:<%=Session.getattribute ("sessionattr") %><BR/>Application:<%=Application.getattribute ("applicationattr") %><BR/>        

When you access the forward page, the result is:

When you visit the redirect page, the result is:

Forwarding differs from orientation:

1, use does not:

Redirect request: 1, call HttpServletRequest's Getrequestdispatcher () method, get the RequestDispatcher object, call Getrequestdispatcher () requires an incoming forwarding address.

2, call the HttpServletRequest forward (Request,response) method.

Forward request: Invokes the Sendredirect () method of the HttpServletResponse object, passing in the Sendredirect () method directly to the destination address to be forwarded.

2. Redirect request: "/" represents the root directory of the current Web site;
Forward request: "/" represents the root directory of the current web App.

3. Redirect request: Can redirect to any resource (including external resources);

Forward Request: A resource that can only be forwarded to the current web app.

4. Redirect request: Only one request was sent. The browser address does not send changes.

Forward request: Two requests were sent. The browser address sends a change.

5. Conversion Request: The destination page can accept the parameter information passed by the page. In the final servlet, the request object and the request in transit are the same object.
Redirect request: In the final servlet, the request object and the requested request in transit are not the same object.

Java Servlet (ix): The difference between a forwarding request and a redirect request

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.