In the previous section, we described the specification of the servlet and the concept and purpose of the servlet and servlet containers, and we know clearly that the servlet container provides a request to receive from the client side. It then processes the request (for example, executes the corresponding servlet to generate dynamic content, or reads static resources, etc.) and finally responds to the client request's resource. In the above process, it is necessary to note that, according to the role of the servlet container, the client side and server side need to transfer data interactively, and the Internet data transmission must be based on some kind of transport protocol, such as HTTP, FTP and so on. The Servelt specification described in the previous chapter is not protocol-based. In this chapter, we will mainly describe the Servlet interface specification based on HTTP protocol transmission. The following is a servlet structure diagram based on the HTTP protocol. Through the above servlet structure class diagram, we need to be aware of the following points:
HttpServlet Enhancements to the servlet specificationHttpServlet is an interface designed to satisfy the servlet specification, which is based on HTTP as a transport protocol. Therefore, it not only satisfies the servlet specification, inherits all the functions in the Servlet (interface methods), and it also has its own unique features, which are designed to handle the information transmitted over HTTP. This may be a bit obscure, or a few examples: for example, HttpServlet not only the service method, it also contains a series of methods Doget, DoPost, Doput, if you are familiar with the HTTP protocol, you should be aware that the HTTP request type has a post , get, put, delete, and so on, while the Doget, Dopost methods in HttpServlet are specifically designed to handle the associated HTTP request types. For example, if you take a closer look at the HttpServletRequest interface, you will find that in addition to inheriting the methods in ServletRequest, it also has GetHeader, GetMethod methods, And GetHeader is specifically used to get HTTP header information, GetMethod is specifically used to obtain the client based on the HTTP protocol request, such as post, get, and so on.
What is the role of Servletrequestwrapper and Httpservletrequestwrapper? Looking at Servletrequestwrapper's class structure diagram, you might find it has two features: 1, which implements the ServletRequest interface; There is a reference to the ServletRequest object in the 2,servletrequestwrapper class. Find out, it is obvious that this is the Java design pattern in the form of decorative mold. Combined with the role of decorative mode, Servletrequestwrapper/httpservletrequestwrapper provides a function to decorate and expand the servletrequest/httpservletrequest. For example If you want to log the operation before calling the Servletrequest.setattribute method, you can create a custom servlet, integrate Servletrequestwrapper, and then override the SetAttribute method to .
What is the role of Servletresponsewrapper and Httpservletresponsewrapper? The effect is similar to the above description. The servletresponsewrapper is used to extract the information that should be responsive to the client, and then make further modifications or enhancements. such as the more commonly used page layout framework Sitemesh is to use this mechanism to capture the content of the page, and then decorate the content.
Servlet Specification Note-HTTP protocol-based servlet