Tuesday, June 3, 2008

GenericServlet, ServletConfig, ServletContext and HTTPServlet


GenericServlet class

As the name suggest, it's a generic servlet and doesn't depend upon a particular protocol. It implements Servlet and ServletConfig interfaces and contains all the base code required by a servlet to maintain its life-cycle. As per the specification, Servlets are not restricted to a particular protocol and we can extend this class for any protocol, but we don't normally do that. Instead we use the already defined subclass (HTTPServlet) of this class specific to the HTTP protocol.

This class contains one abstract method called 'service(ServletRequest request ServletResponse response)'. We just need to define that methods in case we use this class to create servlets. This method is called by the servlet container on receipt of the request for that particular servlet and after that this method takes care of preparing the response for the incoming request.

ServletConfig interface

This interface has methods which are used by the servlet container to pass information to a servlet during the initialization of the servlet. The methods of this interface are:-

  • java.lang.String getInitParameter(java.lang.String name) - used for fetching the initialization parameter. It returns 'null' if the parameter doesn't exist.
  • java.util.Enumeration getInitParameterNames() - used for fetching the names of all the initialization parameters of the servlet. Empty Enumeration is returned in case there is no initialization parameter for the servlet.
  • ServletContext getServletContext() - used for getting the reference to the underlying ServletContext.
  • java.lang.String getServletName() - used for fetching the name of the servlet instance. We can provide this name either via server administration or by specifying that in the Deployment Descriptor of the Web Application, this servlet is a part of. If no name is specified then the class name is returned.

ServletContext interface

This interface has methods which are used for communicating with the servlet container. A Web APplication will have one context per JVM. The ServletContext object is contained within the ServletConfig object and hence every servlet gets it by the Web Server during its initialization. Few of the methods of this interface are:-
  • void log(java.lang.String message) - used for writing the 'message' to a servlet log file.
  • void removeAttribute(java.lang.String nameOfAttr) - used for removing the attribute with the specified name from the servlet context.
  • void setAttribute(java.lang.String name, java.lang.Object object) - used for binding the 'object' with an attribute named 'name'.
  • java.lang.String getServletContextName() - used to fetch the name of the Web APplication of the underlying ServletContext. This name is specified in the Deployment Descriptor by the display-name element.java.lang.
  • Object getAttribute(java.lang.String nameOfAttr) - used for fetching the value of the context's attribute named 'nameOfAttr'.
  • java.lang.String getInitParameter(java.lang.String nameOfParam) - used for fetching the String value of the context-wise initialization parameter with the name as 'nameOfParam'.

HTTPServlet class

This class extends the GenericServlet class to enable itself with not only the basic capabilities of a servlet, but also the HTTP protocol specific other capabilities like Cookies, Sessions, etc.
Similar to the GenericServlet class this class is also abstract and we extend it make the SubClass work like a servlet. The SubClass must override one of the following methods:-
  • doGet() - for HTTP GET requests
  • doPost() - for HTTP POST requests
  • doDelete() - for HTTP DELETE requests
  • doPut() - for HTTP PUT requests
  • init()/destroy() - for managing the resources during the life of the servlet
  • getServletInfo() - this method is used by the servlet to provide information about itself

service() method of HTTPServlet class is not abstract as was the case in the GenericServlet class and there is hardly any need to override that method as by default it contains the capability of deciding the type of the HTTP request it receives and based on that it calls the specific method to do the needful. For example, if an HTTP GET request calls the servlet then the service nethod of the HTTPServlet class determines the request type (it'll find it to be GET) and subsequently calls doGet() method to serve the request.

Difference between GenericServlet and HTTPServlet

HTTPServlet is a sub class of GenericServlet and it contains specific capabilities to handle the requests and responses of a particular protocol called 'HTTP' whereas the GenericServlet is protocol independent and as the name suggests it contains only the generic features which a servlet should have. Currently we use Servlets to handle HTTP requests only, but GenericServlet is capable enough to be extended for any other protocol as well.



Share/Save/Bookmark


No comments: