3.1 OVERVIEW OF SERVLETS API
In earlier chapters we discussed that Servlets are the objects (Java Objects ).Does this mean that any java class can act as Servlets? Answer is No.
For any class to become a Servlet it has to follow the specifications defined for servlets which means class has to implement certain interfaces directly OR indirectly.
3.2 SERVLET PACKAGES
Sun defines two java packages that contain all the interfaces and classes required by Servlet API. These packages are -
- javax.servlet – This package contains generic classes and interfaces (protocol less).
- javax.servlet.http- As package name suggest , this package contains all classes and interfaces related to HTTP protocol.
The main interface in the Servlet API is the Servlet interface. All servlets implement this interface, either directly or indirectly.
By indirectly we mean, instead of implementing Servlet interface, custom servlets can extend one of the two available abstract classes which are Generic Servlet or Http Servlet.
The inheritance hierarchy looks as follows.
Servlet is displayed in a different colour because it is an interface while Generic Servlet and Http Servlet are abstract classes.
Generic Servlet implements Servlet interface whereas Http Servlet extends Generic Servlet.
3.3 SERVLET API
This is the base interface of Java Servlet API and it defines all the lifecycle methods of the servlet.
Since this is an interface, any class implementing Servlet interface has to implement all its methods. Below is the list of methods declared in Servlet interface.
- public abstract void init(ServletConfig) throws ServletException – this is the lifecycle method of servlet and gets invoked only once in the entire lifecycle of a particular servlet. This method is used to initialize the servlet and useful to have onetime processing code or one time initialization code like Database initialization etc.
- public abstract void service(ServletRequest , ServletResponse ) throws ServletException, IOException – This is the main method that performs business logic and responsible for processing client request. When a new request for a servlet comes, servlet container spawns a new thread. Then the thread executes the service() method and executes the business logic or the intended task of the servlet.
- public abstract void destroy() – The destroy() method is called only once at the end of the life cycle of a servlet and thus gives an opportunity to the servlet to perform some cleanup activities like database connection close , closing of any files opened etc. We can relate it to finalize method that gets executed before GC of an object.
- public abstract ServletConfig getServletConfig() – This method returns a servlet config object, which contains any initialization parameters and startup configuration for this servlet.
- public abstract String getServletInfo() – This method returns string containing information about the servlet, such as its author etc.
This is an interface and used to get the servlet initialization and configuration parameters to the servlet. While defining servlet in web.xml, we can provide init parameters and with the help of this interface we can get those init parameters.
“Servlet Config object is specific to Servlet which means each servlet will have its own servlet config object”
To get the Servlet Config object in a servlet, we just need to call getServletConfig() method.
The important methods of ServletConfig interface are:
- public abstract ServletContext getServletContext() – This method returns the ServletContext object for the servlet.
- public abstract Enumeration getInitParameterNames() – This method returns the Enumeration of name of init parameters defined for the servlet.
- public abstract String getInitParameter(String ) – This method returns the value of given init parameter. If parameter is not present with the name, it returns null.
“Key thing to note here is – there is no setInitParameter() method available which means we cannot set or update init parameters from servlet code.”
This is an abstract class and implements both javax.servlet.Servlet and javax.servlet.ServletConfig interface. As its name implies this is a Generic servlet and not tied to any specific protocol.
Since class implements Servlet interface, it provides default implementation of all the methods available in Servlet and ServletConfig interface.
We can simply extend this class to have all the default functionality available and just override the methods of our interest
service() method is still abstract in GenericServlet which means any servlet extending Generic Servlet has to provide implementation of service() method.
“Can you think why Generic Servlet left service method unimplemented? – As mentioned earlier, this method is the main method where complete logic of your servlet goes and how can a generic servlet knows which logic to execute. If this method is not abstract then developers might leave this method unimplemented.”
This class is also an abstract class and extends GenericServlet.
As its name suggests, this class provides base for creating HTTP protocol based web applications.
With Http Servlet, request can be of several types like GET, POST, PUT,DELETE etc. so Http Servlet provides an implementation of service() method as well. Default implementation of service method checks the request type and based on type invokes, doXXX() method where XXX is type of request
There are methods defined to be overridden by subclasses for different HTTP methods in HttpServlet class.
- doGet(), for HTTP GET requests
- doPost(), for HTTP POST requests
- doPut(), for HTTP PUT requests
- doDelete(), for HTTP DELETE requests
Since this class provides an implementation of service method, classes extending Http Servlet class should not override service() method. Instead such classes should override doXXX() methods.
Details of Other Interfaces like ServletRequest, ServletResponse,HttpServletRequest, HttpServletRepsonse will be discussed in a next chapter.
3.6 ADVANTAGES OF EXTENDING HttpServlet OVER IMPLEMENTING SERVLET INTERFACE
- Advantage of extending HttpServlet class over implementing Servlet interface is quite simple .Custom servlet just needs to implement the required methods and will have default implementation out of the box.
- One more advantage is, in case Servlet interface gets changed then all the servlets implementing this interface have to be changed but since HttpServlet or Generic servlet already implements this interface so all of those changes will be already available to use out of the box and there will be no impact on our custom code.
In almost all cases, servlet is created by extending HttpServlet class and implementing doGet() and doPost() method as these two are the most common Request types.