As mentioned in the previous article, Genericservlet is a generic implementation of the Servlet and provides an abstract service () method. And our Internet is using the HTTP protocol to communicate, that for this "special" HTTP protocol, there is no corresponding Servlet implementation?
Yes, it is httpservlet, and it inherits directly from Genericservlet, as shown in.
At the same time, as you can see from the image above, HttpServlet supports 7 Http methods by default:Delete, Head, Get, Options, Post, Put, Trace.
Simple Demo code address: Https://github.com/cyhbyw/JavaWEB_atguigu_TongGang
Project Name: Cyhdebugsource_httpservlet
How does httpservlet inherit the Genericservlet and implement the Http protocol support?
It is easy to see that it converts ServletRequest & Servletresponse directly into HttpServletRequest & in the overridden service () method HttpServletResponse, and calls the overloaded service () method.
This overloaded service () method is implemented as follows. Note that the method into the service () method that participates in the previous rewrite is not the same!
At the same time, it obtains the request form from the HttpServletRequest and calls the corresponding doget (), DoPost (), DoPut () and so on.
Take DoPost () as an example, the implementation is as follows; As you can see, the access level is protected and the default implementation is to return the error directly, so this is a method that is open to subclasses to inherit and implement.
If the subclass does not implement the corresponding method, the request results are as shown.
Conversely, to implement different processing of HTTP requests such as GET, put, post, and so on, it becomes very simple, as shown in
The direct implementation of the corresponding GoGet (), DoPost (), DoPut () and other methods can be, and, the entry is HttpServletRequest & HttpServletResponse, which can be easily handled Http-related content.
====================================================
What's next: How does the Dispatcherservlet in SPRINGMVC work?
====================================================
Java EE httpservlet Parsing