Learn the Servlet API – A step by step approach

The SCWCD(Sun Certified Web Component Developer) exam concentrates on the Servlet API version 2.4.  In order to prepare for the exam its a necessary step to consider to learn the API itself. Learning the API means getting a knowledge about which Interfaces and which Classes exists. Their inheritance relationships and the methods associated with them. This actually helps a person in getting more confident while preparing for the exam since knowledge of the API makes it easy to recognize the errors and exceptions which may occur while using any of the interfaces or classes. It also helps to track the usage of syntaxes when codes are mentioned in the exam and how do they work.

So its in my mind to put forward the API in depth but not make it boring to read (Has anyone ever considered to read the API docs?). So the plan is to learn it in a step by step approach taking into mind the exam related topics!

So here we go.

First of all the important classes / interfaces you need to know are the most basic type i.e of the Servlet. You have the first basic 3 classes.

  1. Servlet – interface
  2. GenericServlet – abstract class
  3. HttpServlet - abstract class

Servlet – Interface

It has the following methods:

void init( ServletConfig config ) throws ServletException    
 
ServletConfig getServletConfig()      
 
void service( ServletRequest req, ServletResponse res ) throws ServletException, IOException      
 
String getServletInfo()      
 
void destroy()
  • The Servlet Interface consists of 5 methods out of which only 2 methods throw exceptions including the main method which is “service” which has to deal with IO.
  •  So it throws an IOException alongwith a ServletException. 
  • Note that destroy does not throw any exception since its job is to just free the memory and it does not effect the usage of the servlet in any way since destroy() is called when the servlet is not used anymore.

GenericServlet – Abstract Class

It implements the Servlet interface. It has 12 methods. Instead of remembering them all, let’s try to group the important ones into categories.

Since this class implements the Servlet interface, it must implement the methods

void init( ServletConfig config ) throws ServletException      
 
ServletConfig getServletConfig()      
 abstract void service( ServletRequest req, ServletResponse res ) throws ServletException, IOException      
 
String getServletInfo()      
 
void destroy()

It gives you 5 convineance methods for providing more functionality to your servlet. The Servlet interface is only a lifecycle manager. So you basically will have init(), service() and destroy(). But GenericServlet is a more a extension to the lifecycle events. It helps a servlet to access the configuration parameters from the web.xml. Hence we have these methods:

public String getServletName ( )      
 
public String getInitParameter ( String name )      
 
public Enumeration getInitParameterNames ( )      
 
public ServletContext getServletContext ( )

GenericServlet also helps the servlet developer to do some tasks which are required just for some custom requirements. This includes some aspects like checking for a database connection before the servlet is instantiated so that the servlet gets created only when the connection is made. So you have your own method init() in this class which is called after the lifecycle event init(ServletConfig cfg) of the Servlet interface. Besides this, you have logging methods which are a necessity of any application.

void  init() 
void  log( String msg ) 
void  log( String message, Throwable t )

Important : You must override the service() method in order to make a GenericServlet subclass since it is declared abstract. The rest methods have default implementations.

HttpServlet - Abstract class

This class extends the GenericServlet abstract class and  implements Serializable class. This is needed if you are making servlets for web application. It has methods which strictly follow the HTTP protocol. It has its own 9 methods besides the inherited methods and all the doXXX() methods are protected.

The methods which correspond to the HTTP protocol are:

protected void  doGet( HttpServletRequest req, HttpServletResponse resp )      
 
protected void  doPost( HttpServletRequest req, HttpServletResponse resp )

Other methods rarely used are:

protected void  doDelete( HttpServletRequest req, HttpServletResponse resp )      
 
protected void  doHead( HttpServletRequest req, HttpServletResponse resp )      
 
protected void  doOptions( HttpServletRequest req, HttpServletResponse resp )      
 
protected void  doPut( HttpServletRequest req, HttpServletResponse resp )      
 
protected void  doTrace( HttpServletRequest req, HttpServletResponse resp )
  • It has a service() method too.
  • This method should not be implemented since it internally sees the type of HTTP request and delegates the request processing to one of the doXXX() methods.
  • If you override this method, then you yourself will have to do the tasks of delegation.
  • All the doXXX() and service() method accept two input parameters which are HttpServletRequest and HttpServletResponse.
protected void  service( HttpServletRequest req, HttpServletResponse resp )      
 
protected long  getLastModified( HttpServletRequest req )

Coming up:
ServletRequest and HttpServletRequest

Leave a Reply

Your email address will not be published. Required fields are marked *