Tuesday, May 7, 2013

servlets based on GenericServlet and HttpServlet

javax.servlet.GenericServlet and javax.servlet.http.HttpServlet are the implementation classes of javax.servlet.Servlet

If we write a servlet based on javax.servlet.Servlet every time we need to provide implementation to all abstract methods of Servlet interface as part of our servlet. Instead of that Sun microsystems has implemented Servlet and released as GenericServlet and HttpServlet.

servlet's based on GenericServlet class:

GenericServlet is an abstract class available as part of javax.servlet package. As part of GenericServlet all abstract methods are implemented except service().

Here is the sample code of GenericServlet(source code)


Here GenericServlet is implementing all the abstract methods except service(). That's why it was declared as abstract class.

We can found two init() methods ( init(ServletConfig config) and init() ) as part of GenericServlet.

init(ServletConfig config) is the first init method and servlet container always calls this method which internally calls the second init method ( init() ).

If we want to perform some task when ever servlet object got created , we need to write code as part of init(). Our servlet will override init(), but container will always call init(ServletConfig config) of GenericServlet, which will call init() of our servlet.

If we create a class extending GenericServlet, it is also known as a servlet  since it is implementing javax.servlet.Servlet interface indirectly.

Here is our servlet based on GenericServlet.


We are implementing abstract method (service()) in our servlet ( SampleServlet ).

Just override init() of GenericServlet in SampleServlet.


So if we observe, when ever container created the object to SampleServlet, it will call init() of SampleServlet instead of init() of GenericServlet since we have overridden the method.

Why init() over init(ServletConfig config) :

It's easy and recommended to override init() than init(ServletConfig config) since there is no need to call super.init(config) if we use init().

Without having ServletConfig object we can simply override init() which will be called by init(ServletConfig config).

servlet's based on HttpServlet class:

HttpServlet is an abstract class available as part of javax.servlet.http package.

Here is the source code of HttpServlet implementation .


As part of HttpServlet service() was implemented as it is abstract as part of GenericServlet.

In HttpServlet two service methods were implemented. service(ServletRequest request, ServletResponse response) is the first service() and service(HttpServletRequest request, HttpServletResponse response) is the second service().

Also doXXX() were provided as part of HttpServlet for every request type (GET,POST,PUT ..etc).

Why two service():

If we use first service(), i.e service(ServletRequest request, ServletResponse response) incase of http request handling we need to type cast ServletRequest , ServletResponse into HttpServletRequest, HttpServletResponse in order to access the methods of HttpServletRequest, HttpServletResponse.

So every time type casting and repeating the same code is not recommended. That's why HttpServlet itself providing two service(), and it's always recommended to use second service(), i.e service(HttpServletRequest request, HttpServletResponse response) as part our servlet .

Here is our servlet(SampleServlet) based on HttpServlet.


We are overriding second service() as part of SampleServlet. Container will always call first service(), i.e service(ServletRequest request, ServletResponse response) which internally calls second service() of SampleServlet.

So, Whenever we are writing a servlet it's always recommended to override second init and second service methods, and container will always call first initand first service methods.


Which one to use either GenericServlet or HttpServlet:

It's always recommended to use HttpServlet while developing servlets because if we use Generic Servlet we need to implement service(ServletRequest request, ServletResponse response) with which we cant access the methods of HttpServletRequest and HttpServletResponse. 
 Every time we need to type cast  ServletRequest,ServletResponse into HttpServletRequest ,HttpServletResponse to access them.

What happens if we did not override any method:

If we have not provided any method as part of our servlet just like as shown.



When client sends the request to the SampleServlet as we have not provided init() container will call init(ServletConfig config) of GenericServlet which internally calls init() of GenericServlet. As part of init() in GenericServlet nothing was coded.

As service() was not overridden as part of SampleServlet, container will call first service of super class(HttpServlet) which internally calls second service() of HttpServlet.

As part of second service() of HttpServlet , calling doXXX() has provided. So if we don't override any method as part of our servlet (SampleServlet), doXXX() will be called based on request method type. If it is GET, it will display error message GET METHOD NOT SUPPORTED  which is available as part of doGet().

So as per the servlet specification we need to override atleast one of these methods :

init(),
destroy(),
service(),
doGet(),
doPost(),
doDelete(),
doPut().


1 comment: