[JavaWeb] Request forwarding and redirection

        In the past two days, I have touched on the two concepts of request forwarding and redirection. Today I will summarize with an example.

Request forwarding: (The browser can be regarded as the destination, and the server can be regarded as the train on the way to the destination )

       For example, I wanted to go to Langfang Station, so I searched for tickets online and found that I could take a train at Binhai Station and get off at Tianjin Station, then reverse in Tianjin and then go to Langfang.

Request forwarding features:

     1) The browser sent a request ( depart directly from Binhai )

     2) Servlet can share request ( Binhai-->Tianjin, Tianjin-->Langfang )

     3) The address bar does not change ( people in Binhai, cars made in Binhai )

     4) The address bar of the browser displays the address of the first visit ( originating address )

     5) You can jump to the project's internal resources (transfer within the station)

     6) The second request is for the internal route ( transfer within the station)

     7) The second request is sent by the server (transfer within the station)

Request forwarding code:

web.xml file: (two servlets, test uses xml development, result uses annotation development)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
    <servlet>
        <servlet-name>TestServlet</servlet-name>
        <servlet-class>com.itheima.Test.TestServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>TestServlet</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>
</web-app>

testServlet:

package com.itheima.Test;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class TestServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // System.out.println("我在滨海站出发");
        //请求转发
        /*
        * 1.获得请求调度器
        * */
        request.setAttribute("first","我在滨海站出发");
        RequestDispatcher rd = request.getRequestDispatcher("result");
        /*
        * 2.调用另一个资源
        * */
        rd.forward(request,response);
    }
}

resultServlet:

package com.itheima.Test;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ResultServlet",urlPatterns="/result")
public class ResultServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取test穿过来的信息
        System.out.println(request.getAttribute("first"));
        System.out.println("到达天津换乘");
        System.out.println("到达廊坊车站");
    }
}

show result:

The test address bar did not change.

Redirect:

        The editor also took the car to Langfang Station, but I didn’t want to back up. I was lazy and afraid of getting lost. When I went to Binhai Station, I found that there was no direct train to Langfang, so I gave up buying tickets in Binhai. The conductor said that Tanggu had a direct train to Langfang, so I gave up on Binhai. Take the car, and then go to Tanggu Station to take the car to Langfang.

Redirection features:

     1) The browser has sent more than two requests ( people are in Binhai, but first go to Binhai without a car, and then send a request to go to Tanggu for a ride )

     2) The servlet cannot share the request ( I wanted to be in Binhai -> Langfang, and then there is no car, so I can't carry this Binhai -> Langfang request, go to Tanggu by car, go to Tanggu, you have to bring Tanggu -> Langfang request)

     3) The address bar changes ( last departure at Tanggu Station )

     4) The address bar of the browser displays the address of the second visit (the original originating address is Binhai, but the final originating address has changed)

     5) You can jump to any resource in the project (the Binhai has no direct access, you can go to Tanggu once, or go to Tianjin once)

     6) The second request is the absolute path (final boarding location)

     7) The second request was sent by the browser (the editor changed the last bus address)

Redirection code:

The xml file configuration remains unchanged!!

testServlet code:

package com.itheima.Test;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class TestServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("first","计划滨海出发");
        response.setStatus(302);
        request.setAttribute("second","滨海没有直达,去塘沽坐车");
        response.setHeader("location","http://localhost:8080/day09/result");
    }
}

resultServlet code:

package com.itheima.Test;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ResultServlet",urlPatterns="/result")
public class ResultServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(request.getAttribute("second"));
        System.out.println("我再塘沽出发");
        System.out.println("到达廊坊车站");
    }
}

The address bar changes by testing:

                             If there are any shortcomings, I hope you can correct me!!!!

Guess you like

Origin blog.csdn.net/weixin_43267344/article/details/108465552