Forward and redirect

Source: Internet
Author: User

This time, you can select your favorite color on the homepage to go to the corresponding page.

Select green to go to the green page:

Select red to go to the Red Page:

Okay. Here we will see four pages:

  1. Select the color in index. jsp and click the button to submit it to test. jsp.

  2. Test. jsp gets the selected color and displays the corresponding page based on the color value.

  3. If Red is selected, Red. jsp is displayed.

  4. If green is selected, Green. jsp is displayed.

In this example, the content in index. jsp, Red. jsp, and green. jsp is the same, and all xuanjicang is in test. jsp.

Now we are faced with the problem of how to decide to implement red. jsp or green. jsp in test. jsp. We can choose either of them in forward and redirect.

3.2 If forward is used

Write in test. jsp as follows:

<%@ page contentType="text/html; charset=gb2312"%><%    String color = request.getParameter("color");    if ("red".equals(color)) {        request.getRequestDispatcher("red.jsp").forward(request, response);    } else if ("green".equals(color)) {        request.getRequestDispatcher("green.jsp").forward(request, response);    }%>        

Skip parameter acquisition and comparison parameter values, and focus only on the forward section:

request.getRequestDispatcher("red.jsp").forward(request, response);        

First, call the getrequestdispatcher () method of the request to obtain the forwarder corresponding to Red. jsp, and then call the forward () method to execute request forwarding. The result is the result in red. jsp, a red page.

Note the URL of the browser:

When the red page is selected:

When you select a green page:

Therefore, no matter whether it is forwarded to Red. jsp or green. jsp, the address bar displays test. jsp.

Why? The following flowchart makes it easy to understand:

  1. The browser sends a request to test. jsp.

  2. Test. jsp calculates the color selected by the customer and forwards the request to Red. jsp.

  3. Red. jsp returns a response to the browser.

Now, why does the browser address remain unchanged? Because the browser only executes test. jsp request, test. JSP to red. the JSP part is executed on the server. The browser does not know what happened on the server. It only knows that the response it obtains is test. JSP sent back, even do not know the server has a red. JSP.

This is the forward request. For example, see lingo-Sample/03-01 /.

3.3. If redirect is used

Write in test. jsp as follows:

<%@ page contentType="text/html; charset=gb2312"%><%    String color = request.getParameter("color");    if ("red".equals(color)) {        response.sendRedirect("red.jsp");    } else if ("green".equals(color)) {        response.sendRedirect("green.jsp");    }%>        

Skip parameter acquisition and comparison parameter values, only focus on the redirect part:

response.sendRedirect("red.jsp");        

Response is the response, representing the HTTP response. Call the sendredirect ("red. jsp") method of response to redirect the page to Red. jsp.

Pay attention to the URL of the browser:

When the red page is selected:

When you select a green page:

Unlike forward, the URL address remains changing. Red. jsp is displayed in red, and green. jsp is displayed in green.

Let's look at the process diagram again:

  1. The browser sends a request to test. jsp.

  2. Test. jsp calculates the customer's selected color and sends a page redirect response to the browser. The response contains the URL of red. jsp.

  3. The browser sends a request to the server again based on the red. jsp address in the response to the page redirect (redirect). The request is red. jsp.

  4. Run Red. jsp and return a response.

Redirect will trigger another request response process. In the second request, the browser initiates a request to Red. jsp, so the URL address is changed.

This is page redirection redirect. For example, see lingo-Sample/03-02 /.

3.4. Forward and redirect problems 3.4.1. absolute path and relative path
  1. If the URL we use starts with "/", this URL is called an absolute path.

  2. If the URL we use does not start with "/", the URL is called a relative path.

3.4.1.1. Relative Path

In relative paths, the two are the same.

Let's take a look at the lingo-Sample/03-03/example. If we request relative/forward. JSP or redirect. JSP, and then jump to the result/result below it. what Will JSP do?

  1. Forward example:

    <%request.getRequestDispatcher("result/result.jsp").forward(request, response);%>                        

    The relative path here is result/result. jsp.

    Because the test. JSP is under/03-03/relative/, so our current path is/03-03/relative/. When we execute forward, we will find the result/result under the current path. JSP, and then forward the request.

  2. Redirect example:

    <%response.sendRedirect("result/result.jsp");%>                        

    The relative path here is also result/result. jsp.

    Because the test. JSP is under/03-03/relative/, so our current path is/03-03/relative/. When executing redirect, the current path will be added with result/result. JSP, send the result to the browser as the redirection address, and then request/03-03/relative/result. JSP to get the response.

3.4.1.2. absolute path

When the problem arises, the absolute path is different in forward and redirect, but it is still the case just now, but it is different in writing when using the absolute path.

  1. Forward example:

    <%request.getRequestDispatcher("/relative/result/result.jsp").forward(request, response);%>                        

    The absolute path here is/relative/result. jsp.

    During local testing, forward regards http: // localhost: 8080/03-03/as the root path and calculates the absolute path based on it.

    This is determined by the JSP deployment method. Many projects can be stored in the webapp, so that these projects can run independently without affecting each other, requests cannot be directly transferred from one project to another within the server. To prevent this situation, the project path is treated as the root directory when forward is executed, so that the developer will not see other projects and the problem will not occur.

  2. Redirect example:

    <%response.sendRedirect("/03-03/absolute/result/result.jsp");%>                        

    The absolute path here is/03-03/absolute/result. jsp.

    During local testing, redirect regards http: // localhost: 8080/as the root path and calculates the absolute path based on it.

    Because the RDBMS will edire the browser to re-initiate a new request, it will not disturb the relationship between multiple projects on the server, so it does not need to be limited, if you need to jump between multiple projects, you can only use redirect. However, because a new request is initiated again, the data in the previous request will be lost. If there is any important data, remember to reset it.

3.4.2. Forward: The image cannot be found.

This problem occurs if no image is found, no JS script is found, and no CSS style sheet is found.

To demonstrate this problem, it is very easy to meet only two conditions:

  1. The JSP pages before and after forward are not in the same directory.

  2. The JSP page after forward uses relative paths to reference resources, images, JS scripts, CSS style sheets, and so on.

In 03-04, this environment is simulated. You enter http: // localhost: 8080/03-04/and select "problematic ":

Open 03-04 and you can see the following directory structure:

|--+ 03-04   |--- index.jsp   |--- test.jsp   |--+ result      |--- success.jsp      |--- failure.jsp      |--- lingo.png            

The page we just saw is failure. jsp, where the image is shown as follows:

<img src="lingo.png" />            

At this time, I am wondering why lingo.png and failure. jsp are clearly in the same directory.

Right-click an image that cannot be displayed and select Properties. Let's see a piece of request address:

The image is originally located at http: // localhost: 8080/03-04/result/lingo.png, but the request address is http: // localhost: 8080/03-04/lingo.png. The problem is that the intermediate/result is lost.

Try again "no problem" on index. jsp ":

The page we see this time is success. jsp, which displays the following picture:

<img src="result/lingo.png" />            

After the result is manually added to the result path, the image is displayed.

This problem should be traced back to the way the browser handles HTML. external resources such as images, CSS style sheets, JS scripts, and videos contained in HTML must be requested by the browser again to the server.

If these external resources use relative paths, the browser concatenates a complete HTTP request based on the current request path and sends it to the server. In this example, we request http: // localhost: 8080/03-04/test. in JSP, the current path obtained by the browser is http: // localhost: 8080/03-04/. The relative region of the image in failure.jspis lingo.png, and the result of the splicing is http: // localhost: 8080/03-04/lingo.png.

Do not blame the browser for being too stupid because the browser does not know these changes when using forward. It has always believed that, since test. jsp is requested, the content of test. jsp is naturally returned, it is no problem to use test. jsp as the current path to calculate the relative path. We cheated the browser, secretly changed the request flow on the server, and returned the content of other pages.

After understanding the above request process, you will know how to deal with this problem.

  1. Method 1: Do not use forward for request forwarding between different directories to ensure that the current path does not change.

  2. Method 2: Modify the image path, or change all to the absolute path, as in the previous example.

Select as needed.

 

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.