For example, the difference between redirection and request forwarding, and for example, redirection forwarding

Source: Internet
Author: User

For example, the difference between redirection and request forwarding, and for example, redirection forwarding
Redirection

  HttpServletResponseObjectSendRedirect (java. lang. String location)The method is calledRedirection.

If "/" is added before the location address, it indicates a request relative to the Servlet container's root, for example, http: // localhost: 8080. If "/" is not added before the location address, it indicates to look for the address relative to the URI of the current request.

 

Request forwarding

  RequestDispatcherOf:Forward (ServletRequest request, ServletResponse response)The method is request forwarding.

 

Tutorial 1: redirection and request forwarding both seem to cause page Jump

  First pageFirst. 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">

 

  Second pageYesservlet:

Forward requests:

 

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. extends; public class Second extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {process (request, response);} public void doPost (HttpServletRequest request, response) throws ServletException, IOException {process (request, response);} private void process (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// request forwarding RequestDispatcher rd = request. getRequestDispatcher ("third. jsp "); rd. forward (request, response );}}

 

Use redirection to change the processing method:

Private void process (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// redirect response. sendRedirect ("third. jsp ");}

 

  Third pageIs 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 whether request forwarding or redirection is used, after you click submit on the first page, you can smoothly go to the third page:

However, the actual operations are quite different. Let's look at the example below.

 

 

Tutorial 2: To obtain the username entered on the first page on the third page

  Request forwardingThe implementation is relatively simple. The Servlet code on the second page does not need to add anything. You can directly change the body in third. jsp from getParameter on the third page:

 

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

Enter shengqishiwind on the first page. After submission, the third page is displayed:

  How can I get the username submitted on the first page for redirection?

The third page uses the code above (get the parameter through getParameter), and changes the second page to the redirection redirect mode. The page is displayed as follows:

Try the second page (store the value through setAttribute) as follows ):

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 is retrieved as follows:

Username: <% = request. getAttribute ("username") %>

It still does not work. The resulting value is still null.

 

 

What is the difference between redirection and request forwarding?

Request forwarding:

RequestDispatcher is obtained by calling the getRequestDispatcher () method of the HttpServletRequest object and is a method of the request object.

  Request forwarding: the whole process is in the same request. 

No matter how many components you have experienced, you can directly obtain the desired value from the request.

When forwarding requests, the URL to the result page is: http: // localhost: 8080/HelloWeb/Second? Username = wind

 

Redirection: 

SendRedirect () is the method of the HttpServletResponse object, that is, the method of the response object.

Since the method of the response object is called, it indicates that the entire request process has ended and the server starts to return the execution result to the client.

After sendRedirect is called, a response is returned to the client, which tells the client the page to be switched, and then the client sends a new request to the target page.

That is to say,In the redirection process, the client actually sends two requests to the server.

 

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

It indicates that the client has known the address of the result page and is a brand new request for resending.

InFirebugGraph in:

 

 

  Summary:Request forwardingThere is only one request. Therefore, the method called is forward.RedirectionThe client needs to Re-send the request. sendRedirect is called, And a Re in the name indicates repeated requests.

Related Article

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.