The javax. servlet. genericservlet abstract class implements the servlet interface. The genericservlet abstract class provides a general implementation for the servlet interface, and it has nothing to do with any network application layer protocol. In addition to servlet interfaces, the genericservlet class also implements the servletconfig interface and serializable interface. Genericservlet classSource code(Servlet interface implementationCode) As follows:
Public abstract class genericservlet implements servlet, servletconfig, Java. io. serializable {private transient servletconfig config; Public genericservlet () {} public void Init (servletconfig config) throws servletexception {This. config = config; this. init ();} public void Init () throws servletexception {} public abstract void Service (servletrequest req, servletresponse res) throws servletexception, ioexception; Public void destroy () {} Public String getservletinfo () {return "";} public servletconfig getservletconfig () {return config ;}}
From the source code of the genericservlet class, we can see that the genericservlet class implements the init (servletconfig config) initialization method in the servlet interface. The genericservlet class has a private instance variable config of the servletconfig type. When the servlet container calls the init (servletconfig config) method of genericservlet, this method allows the private instance variable config to reference the servletconfig object passed in by the container, even if the genericservlet object is associated with a servletconfig object.
The genericservlet class also defines an Init () method without parameters. The init (servletconfig config) method calls this method. For the subclass of the genericservlet class, if you want to override the initialization behavior of the parent class, there are two methods.
First: override the init () method without parameters of the parent class: Public void Init () {// initialization behavior of the subclass .......} second, override the parameter Init (servletconfig config) method of the parent class. If you want to associate the current servlet object with the servletconfig object, you should first call super. init (config) method: Public void Init (servletconfig config) {super. init (config); // call the init (config) method of the parent class // initialize the subclass .......}
The genericservlet class does not implement the service () method of the servlet interface. The Service () method is the only abstract method in the genericservlet class. The specific subclass of the genericservlet class must implement this method to provide specific services for specific customer requests.
Although the genericservlet class implements the destroy () method of the servlet interface, nothing is actually done. The specific subclass of genericservlet can overwrite this method to release the resources occupied by the current servlet object to be destroyed (for example, closing the file output stream and input stream, and closing the connection to the database ).