The Servlet object is a Java object that is initiated and managed by the Servlet container. However, the Java object needs to be implemented descriptive to the Servlet container. The following rules are specified by the Java Servlet specification for implementing the Servlet object:
• Should be a public non-abstract class.
• Should be a subtype of the javax.servlet.Servlet interface, as the Servlet container understands the Servlet object by using this interface.
• Should support a no-argument constructor.
• Recommended not to declare the methods of the javax.servlet.Servlet interface implemented by the Servlet object as final.
As we understood the rules for implementing servlet object, let us learn about the javax.servlet.Servlet andjavax.servlet.ServletConfig interfaces,which arethe basic and important elements of Java Servlet API to understand the Servlet life cycle.
We’ll be covering the following topics in this tutorial:
ServletConfig OBJECT
• It is one per Servlet Class Object so it is called right hand object of the servlet class object.
• ServletContainer creates this object along with our servlet class object and also destroys this object along with our Servlet class object.
• This is the object of underlying Servlet Container Supplied java class that implements javax.servlet.ServletConf ig interface.
• This object is useful to gather details about Servlet program and to pass information to servlet program.
• This object is useful to gather unit parameter values from web.xml file.
• This object is required to get access to ServletContext object.
ServletContext Object
• It is one per web application and visible to all web resource programs of the web application, it is called global memory of the web application.
• Servlet Container creates this object either during server startup (cold deployment) or during deployment of the web application (hot deployment) using this object.
(a) We can get underlying server info.
(b) We can get details of the web resource program of the web.
(c) We can get servlet API version that server uses.
(d) We can get streams pointing to the resource program of the web application.
(e) We can write message to long files.
•Different ways of access to ServletConfig obj.in our Servlet program.