Calling rules between multiple servlets

Prerequisites

        Some requests are sent from browsers, which often require multiple Servlets on the server to process collaboratively.

        But our browser can only access one Servlet at a time, causing our users to manually

        Only by initiating multiple requests through the browser can the service be obtained.

Rules to improve user experience

        No matter how many Servlets are involved in this request, the user only needs to [manually] notify the browser to initiate a request.

Rules for calling each other between multiple servlets

        1) Redirection solution

        2) Request forwarding solution

Redirection solution

1. Working principle:

        The user first informs the browser to access OneServlet by [manually].

        After the OneServlet work is completed, write the TwoServlet address into the response header

        The location attribute causes Tomcat to write the 302 status code into the status line.

        After the browser receives the response packet, it will read the 302 status code. At this time, the browser

        Will automatically initiate a second request based on the location attribute address in the response header,

        Visit TwoServlet to complete the remaining tasks in the request.

2. Realize the command:

        response.sentRedirect("Request address");

        Write the address to the location attribute in the response header in the response packet.

3. Features:

        1) Regarding the requested address:

            That is, the address of the resource file inside the current website can be sent to the browser ( /website name/resource file name )

            You can also send resource files from other websites to the browser (http://ip address: port number/website name/resource file name )

        2) Number of requests:

            The browser has to send at least two requests, but only the first request is manually sent by the user.

            Subsequent requests are automatically sent by the browser.

        3) Request method:

            In the redirect solution, the browser is notified through the address bar to initiate the next request, so

            The request method received by the resource file called by the redirection solution must be [GET]

        4) Disadvantages:

            The redirection solution requires multiple round trips between the browser and the server, and a lot of time

            Consume on the number of round trips, increasing the user's waiting time

Code display


public class OneServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("OneServlet is running....");
        //重定向将解决方案
        //response.sendRedirect("/myWeb/two");
        response.sendRedirect("http://www.baidu.com");
    }
}


public class TwoServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("TwoServlet is running.....");
    }
}

Request forwarding solution

1. Working principle:

        The user requests the browser to access OneServlet through the [manual] method for the first time.

        After OneServlet finishes its work, it replaces the browser with the current request object

        Send a request to Tomcat to apply to call TwoServlet.

        After Tomcat receives this request, it automatically calls TwoServlet to complete

        The remaining tasks.

    2. Implement the command: let the request object send a request to the Tomcat server instead of the browser

        //Generate a resource file application report through the current request object

        RequestDispatcher report=request.getRequestDispatcher("/The name of the resource file"); Must start with a slash "/"

        //Send this report object to Tomcat

        report.forward (current request object, current response object);

    3. Advantages:

        1) No matter how many servlets are involved in this request, the user only needs to manually send a request through the browser once

        2) The calls between servlets occur on the server computer, which saves the number of round trips between the server and the browser

        Increase the speed of processing services .

    4. Features:

        1) Number of requests:

            During the request forwarding process, the browser only sent the request once

        2) Request address:

            You can only apply to the Tomcat server to call the resource files under the current website

            request.getRequestDispatcher("/resource file name"); ****** Do not write website name *******

        3) Request method:

            During the request forwarding process, the browser only sent an Http request protocol packet.

            All Servlets participating in this request share the same request protocol.

            The request mode received by these servlets is consistent with the request mode sent by the browser.

Code display

public class OneServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("OneServlet is doing.......");
        //通过请求对象生成资源文件申请报告
        RequestDispatcher report=null;
        report= request.getRequestDispatcher("/two");
        report.forward(request,response);
    }
}


public class TwoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("TwoServlet is running......");
    }
}

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_45796208/article/details/108725630