GenericServlet and HttpServlet

GenericServlet :

It provides basic lifecycle methods for servlets. GenericServlet class implements Servlet, ServletConfig and Serializable interfaces.
It provides the implementation of all the methods of these interfaces except the service method.
GenericServlet class can handle any type of request so it is protocol-independent.
It inherits all the methods of Servlet and ServletConfig for creating and handling Servlets.Methods of GenericServlet class:

Method
Use
public void log(String message)
writes the name of servlet and message argument to the web container’s log.
public void log(String message, Throwable t)
writes the name of servlet and message argument to the web container’s log and include the stack trace for the given Throwable exception.

Example :

index.html

Preview.java

web.xml

HttpServlet :

The HttpServlet class extends the GenericServlet class and implements Serializable interface. It provides http specific methods such as doGet, doPost, doHead, doTrace etc. It is commonly used for servlets that receive and process HTTP requests.

Methods of HttpServlet class :

Method
Use
protected void doGet(HttpServletRequest request, HttpServletResponse response)
handle the GET request.
protected void doPost(HttpServietRequest request, HttpServletResponse response)
handle the POST request.
protected void doHead(HttpServietRequest request, HttpServletResponse response)
handle the HEAD request.
protected void doOptions(HttpServietRequest request, HttpServletResponse response)
handle the OPTIONS request.
protected void doPut(HttpServietRequest request, HttpServletResponse response)
handle the PUT request.
protected void doTrace(HttpServietRequest request, HttpServletResponse response )
handle the TRACE request.
protected void doDelete(HttpServietRequest request, HttpServletResponse response)
handle the DELETE request.
protected void  service(HttpServletRequest request, HttpServletResponse response)
calls the one of the above methods, depending on the type of the HTTP request method.
protected long getLastModified(HttpServletRequest request)
returns the time when the servlet was last was last modified In milliseconds
public void service(ServletRequest request,ServletResponse response)
This is an implementation of the service method in the GenericServlet. And this service method calls the protected service() method after casting the arguments to HttpServletRequest and HttpServletResponse.

Example :

index.html

Preview.java

web.xml

Difference Between GenericServlet and HttpServlet:

GenericServlet
HttpServlet
GenericServlet are protocol independent
HttpServlet are protocol dependent.
GenericServlet are incompatible with HTTP methods
HttpServlet are used to handle HTTP requests and response.
For each and every request type (Get,Post,Put,..) GenericServlet will use same execution process
HTTPServlet will separate execution process for each and request type.

Leave a Reply

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