The Servlet interface is the root interface of the servlet class hierarchy. All Servlets need to either directly or indirectly implement the Servlet interface. The GenericServlet class of the Servlet API implements the Servlet interface. In addition to the Servlet interface, the GenericServlet class implements the ServletConfig interface of the Servlet API and the Serializable interface of the standard java.io. package. The object of the ServletConfig interface is used by the Web container to pass the configuration information to a servlet when a servlet is initialized.
To develop a servlet that communicates using HTTP, we need to extend the HttpServlet class in our servlet. The HttpServlet class extends the GenericServlet class and provides built-in HTTP functionality.
The javax.servlet.Servlet Interface
The servlet interface of the javax.servlet package defines methods that the Web container calls to manage the servlet life cycle. The following table describes various methods of the javax.servlet.Servlet interface:
Method |
Description |
public void destroy() | The Web container calls the destroy() method just before removing the servlet instance from service. |
public ServletConfig getServletConfig() | Returns a ServletConfig object that contains configuration information, such as initialization parameters, to initialize a servlet. |
public String getServletInfo() | Returns a string that contains information about the servlet, such as author, version, and copyright. |
public void init(ServletConfig config) throws ServletException | The Web container calls this method after creating a servlet instance. |
The javax.servlet.ServletConfig Interface
The javax.servlet.ServletConfig interface is implemented by a Web container to pass configuration information to a servlet, during initialization of a servlet. A servlet is initialized by passing an object of ServiceConfig to its init() method by the Web container. The ServletConfig object contains initialization information and provides access to the ServletContext object.
Initialization parameters are name value pairs that we can use to pass information to a servlet. The following table lists some of the methods of the javax.servlet.ServletConfig interface:
Method |
Description |
public String getInitParameter(String param) | Returns a String containing the value of the specified initialization parameters or null if the parameter does not exist. |
public Enumeration getInitParameterNames() | Returns the names of all initialization parameters as an Enumeration of String objects. If no initialization parameters have been defined, an empty Enumeration is returned. |
public ServletContext getServletContext() | Returns the ServletContext object for the servlet, which allows interaction with the Web container. |
Leave a Reply