Request forwarding + request contains + request domain object

What is request forwarding

Request forwarding implements resource jump within the server.

Characteristics of request forwarding

One request, one response. A request object must correspond to a response object.

The resources jump inside the Web application (inside the server), the browser does not feel the change, and the address bar will not change.

Request forwarding to realize server internal resource jump

In demo4

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Get the scheduler, use the scheduler to achieve forwarding
        request.getRequestDispatcher("/RequestDemo5").forward(request,response);
    }

In demo5

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().write("here is RequestDemo5!");
    }

Enter in the browser: http://localhost/demo/RequestDemo4.

Since it is internal to the server, the path path can be written directly to the / resource name.

Request forwarding and data flushing

ok, request forwarding is still relatively clear. Now there is a new requirement. If I want to display a piece of data on the demo4 page, forward it to demo5, and then display another piece of data, how can I achieve it?

What happens if data is written first in demo4 and then forwarded?

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Write cache before retransmission
        response.getWriter().write("cache");
        //Get the scheduler, use the scheduler to achieve forwarding
        request.getRequestDispatcher("/RequestDemo5").forward(request,response);
    }

demo5 does not change. If you visit http://localhost/demo/RequestDemo4 again, you will see the following results, not the cache.

This is because if there is data in the response buffer before the request is forwarded, the buffer will be cleared when the request is forwarded.

If I call response.flushBuffer() directly after writing, will I solve the problem by first flushing the data to the browser for display and then forwarding it? Try changing demo4, demo5 does not change.

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Write cache before retransmission
        response.getWriter().write("cache");

        //Call the flushBuffer method to flush the buffer
        response.flushBuffer();
        //Get the scheduler, use the scheduler to achieve forwarding
        request.getRequestDispatcher("/RequestDemo5").forward(request,response);
    }

On another visit, the following conditions occurred:

Calling the flushBuffer() method to flush the response buffer will cause a response operation, and because a request in the request forwarding model will correspond to a response, the response of the request forwarding cannot be realized. Similarly, if another request is forwarded and then flushed, the flushing operation cannot be performed.

Normal execution before and after request forwarding statement

demo4:

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Write cache before retransmission
        response.getWriter().write("cache");

        System.out.println("demo4 start");
        //Get the scheduler, use the scheduler to achieve forwarding
        request.getRequestDispatcher("/RequestDemo5").forward(request,response);

        System.out.println("demo4 end");
        //Call the flushBuffer method to flush the buffer
        response.flushBuffer();
    }

demo5:

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("demo5 start");
        response.getWriter().write("here is RequestDemo5!");
        System.out.println("demo5 end");
    }

When accessing demo4, the console will output the following results:

demo4 start
demo5 start
demo5 end
demo4 end

It can be seen that the statements before and after forwarding are executed normally and in a certain order.

Request forwarding can be multiple forwarding

Forward to demo6 in demo5, the previous code remains the same:

request.getRequestDispatcher("/RequestDemo6").forward(request,response);

demo6:

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("demo6 start");
        response.getWriter().write("here is demo6");
        System.out.println("demo6 end");
    }

Finally, visit demo4, and the results are as follows:

Console output:

demo4 start
demo5 start
demo6 start
demo6 end
demo5 end
demo4 end

Request includes

Suddenly I think of the demand just now. The content of both pages should be displayed. What should I do? Request include!

demo4 medium

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Write demo4 before retransmission
        response.getWriter().write("demo4 ");
        //Get the scheduler, use the scheduler to achieve forwarding
        request.getRequestDispatcher("/RequestDemo5").include(request,response);
    }

demo5 medium

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().write("demo5");
    }

The requirements are now complete.

What is the domain object

In a domain, the object for data sharing, such as the request object, is created when the server receives the request and forwarded multiple times within the server. In the same request chain, multiple Servlet resources share the request, and the request is a domain object.

In essence, data sharing is implemented by the Attribute name and Attribute value of the object map, so we can see that we need methods about attributes.

Domain object related methods

Implementation: set the property and property value in demo7, and get the value in demo8.

/**
 * @auther Summerday
 *
 * request Domain object
 */
@WebServlet("/RequestDemo7")
public class RequestDemo7 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //Add data to domain
        request.setAttribute("message","hello,i'm demo7");
        request.setAttribute("name","summerday");
        request.setAttribute("age",18);
        //Forward
        request.getRequestDispatcher("/index.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
/**
 * @auther Summerday
 */
@WebServlet("/RequestDemo8")
public class RequestDemo8 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Get the names of all domain properties
        Enumeration<String> names = request.getAttributeNames();
        while(names.hasMoreElements()){
            String s = names.nextElement();
            //Gets the domain property of the specified name
            String value = (String) request.getAttribute(s);
            System.out.println(s+">>"+value);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

Lifecycle of domain objects

At the beginning of a request, the request object will be created. At the end of the request chain (which may contain multiple forwarding), the request object will be destroyed. The scope of the request chain is the scope of the request domain object.

101 original articles published, 79 praised, 7669 visited
Private letter follow

Tags: Attribute JSP

Posted on Mon, 16 Mar 2020 11:21:45 -0400 by Pro Ninja