In the servlet, using the RequestDispatcher object, you can forward the request to another servlet or JSP page, or even an HTML page, to handle the response to the request.
Introduction to RequestDispatcher interface method
The 1,requestdispatcher object is created by the servlet container to encapsulate a server resource identified by the path.
Two methods are defined in the 2,requestdispatcher interface for request forwarding:
Forward (Servletrequest,servletresponse) method:
Forward the request to another servlet,jsp page on the server, or the method of HTML file must be called before the response is submitted to the client, otherwise an exception will be thrown.
Content that is not committed in the response after the method call is automatically eliminated.
include (Servletrequest,servletresponse) method:
Used to include the contents of other resources (servlet,jsp pages or HTML files) in the response.
That is, after the request is forwarded, the original servlet can continue to output the response information, and the forwarded Servlet's response to the request is merged into the original servlet's response object.
The difference between the 3,forward method and the Include method:
Forward content that is not committed in the response after the method call is automatically eliminated. The Include method enables the original servlet and the servlet to be forwarded to output the response information.
two, get the RequestDispatcher object
three ways to get RequestDispatcher objects:
1, using the Getrequestdispatcher (String path) method in the ServletRequest interface.
Getnameddispatcher (string path) and Getrequestdispatcher (string path) methods in the 2,servletcontext interface.
the difference between the Getrequestdispatcher methods in the ServletRequest interface and the ServletContext interface :
1, the difference between the parameters
Although the parameters are both resource pathname, the ServletContext interface's medium parameter path must start with "/" and is relative to the current servlet context root, and the parameter path in the ServletRequest interface can not only be compared to the current servlet context root, You can also compare to the current servlet path
2, accessing resources across Web applications
The Servletcontext.getcontext () method Gets the context object of the other Web application to invoke the Getrequestdispatcher (String Path) method to forward the request to a resource for another Web application.
You also need to set the <context> element in the current Web application configuration, specifying that the Crosscontext property value is true.
Three, the difference between the Sendreadirect () method and the Forward () method in the Servletresqonse interface
All two methods are used for request forwarding, and are forwarded to another resource for client service. But there are essential differences between the two.
Sendreadirect () method principle:
1, the client sends the request, SERVLET1 to make the processing.
2,servlet1 calls the Sendreadirect () method to reposition the client 's request to Servlet2.
3, the client browser accesses the Servlet2.
4,servlet2 responds to the client browser.
forward () method principle:
1, the client sends the request, SERVLET1 to make the processing.
2,servlet1 calls the Sendreadirect () method, forwarding the request to the Servlet2 to process the request and service the client.
3,servlet2 responds to the client browser.
difference:
1, positioning and forwarding
The Sendreadirect () method is to relocate to another resource to process the request, and the URL will be relocated to allow the client to access another resource again. The forward () method is forwarded to another resource to process the request. The URL does not change. Hides the change in the processing object.
2, the scope of the resource to process the request
The Sendreadirect () method can relocate resources across Web applications and servers to process requests. The forward () method can only be forwarded within the application.