Genericservlet abstract class implements the servlet Interface

Source: Internet
Author: User

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 ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.