Servlet forwarding, including detailed explanation (VII)
This paper introduces servlet forwarding and servlet inclusion for your reference. The specific contents are as follows
1. What is servlet forwarding?
Servlets process users' requests through a group of servlets. Each servlet has its own specific responsibilities. When the servlet completes its work, it forwards the request to the next servlet for processing. This process of forwarding requests to complete the processing of a request through different servlets is called servlet forwarding.
2. What is servlet inclusion?
Servlet inclusion means that the source servlet includes the results generated by other web components (servlets, HTML, JSP) into its own results.
3. Why do I need servlet forwarding and inclusion?
Servlet objects are created by servlet containers, The service () method of the servlet object is called by the servlet container, and servlets are not allowed to call each other (servlet API versions before 2.1 can). However, we need to coordinate and complete tasks through multiple servlet components, facilitate code maintenance and management, and improve development efficiency. In this way, we need servlet forwarding and included technical support.
4. Similarities between forwarding and inclusion
(1) both processes the same request object. The source component and the target component share the same ServletRequest object and servletresponse object.
(2) the target components can be servlets, JSPS and HTML.
(3) both rely on javax and servlet.requestdispatcher interface.
5. Requestdispatcher object
This interface represents the request distributor
(1) get the requestdispatcher object method
Call the getrequestdispather (string path) method of ServletContext to obtain.
Call the getrequestdispather (string path) method of ServletRequest to obtain.
(2) differences between the two methods
The parameters of the calling method of ServletContext must be absolute path, and the parameters of the calling method of ServletRequest can be absolute path or relative path.
6. Request forwarding case
The checkservlet class is used to process user request parameters.
Outservlet is used to output processing results
Note: 1 Do not submit response results before forwarding.
2. During forwarding, the response result of the source component will not be returned, but only the response result of the target component.
web. XML configuration
7. Include cases
Mainservlet is used to contain other components and return response results
header. html
GreetServlet
footer. html
web. XM configuration
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.