Difference Between GenericServlet and HttpServlet

Generic Servlet:

  • GenericServlet is an abstract class which implements Servlet, ServletConfig and java.io.Serializable interfaces.
  • GenericServlet belongs to javax.servlet package.
  • Generic Servlet supports  only service() method. Extending class must override public abstract void service(ServletRequest req,ServletResponse res ) method.
  • GenericServlet implements ServletConfig interface and provides way to accept initialization parameter passed to Servlet from web.xml e.g. by using getInitParamter().

GenericServlet Example

import java.io.*;
import javax.servlet.*;

public class GenericServletExample extends GenericServlet
{
     public void service(ServletRequest req,ServletResponse res)
     throws IOException,ServletException
     {
          res.setContentType("text/html");
          PrintWriter out=res.getWriter();
          out.print("<html>");
          out.print("<body>");
          out.print("<h2>Generic Servlet Example!!!</h2>");
          out.print("</body>");
          out.print("</html>");
     }
}

HttpServlet Class:

  • HttpServlet is protocol dependent. It supports only http protocol.
  • HttpServlet class is the direct subclass of Generic Servlet.
  • HttpServlet is an abstract class which extends GenericServlet and implements java.io.Serializable interface.
  • HttpServlet belongs to javax.servlet.http package.
  • HttpServlet overrides service() method of Generic Servlet and provides callback on doXXX(HttpServletRequest request, HttpServletResponse) method whenever it receives HTTP request, it supports doGet(), doPost(), doPut(), doDelete(), doHead(), doTrace(), doOptions() methods.
  • HttpServlet  has  two service methods public void service(ServletRequest req,ServletResponse res ) and protected void service(HttpServletRequest req,HttpServletResponse res) All the request first goes to the public service() method, which wrap into Http Objects and calls protected service() method
     public void service(ServletRequest req, ServletResponse res)
    	throws ServletException, IOException
        {
    	HttpServletRequest	request;
    	HttpServletResponse	response;
    	
    	try {
    	    request = (HttpServletRequest) req;
    	    response = (HttpServletResponse) res;
    	} catch (ClassCastException e) {
    	    throw new ServletException("non-HTTP request or response");
    	}
    	service(request, response);
        }

    which inturn calls the corresponding  doXXX(HttpServletRequest request, HttpServletResponse) method.

     protected void service(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException
    {
	String method = req.getMethod();

	if (method.equals(METHOD_GET)) {
	    long lastModified = getLastModified(req);
	    if (lastModified == -1) {
		// servlet doesn't support if-modified-since, no reason
		// to go through further expensive logic
		doGet(req, resp);
	    } else {
		long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
		if (ifModifiedSince < (lastModified / 1000 * 1000)) {
		    // If the servlet mod time is later, call doGet()
                    // Round down to the nearest second for a proper compare
                    // A ifModifiedSince of -1 will always be less
		    maybeSetLastModified(resp, lastModified);
		    doGet(req, resp);
		} else {
		    resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
		}
	    }

	} else if (method.equals(METHOD_HEAD)) {
	    long lastModified = getLastModified(req);
	    maybeSetLastModified(resp, lastModified);
	    doHead(req, resp);

	} else if (method.equals(METHOD_POST)) {
	    doPost(req, resp);
	    
	} else if (method.equals(METHOD_PUT)) {
	    doPut(req, resp);	
	    
	} else if (method.equals(METHOD_DELETE)) {
	    doDelete(req, resp);
	    
	} else if (method.equals(METHOD_OPTIONS)) {
	    doOptions(req,resp);
	    
	} else if (method.equals(METHOD_TRACE)) {
	    doTrace(req,resp);
	    
	} else {
	    //
	    // Note that this means NO servlet supports whatever
	    // method was requested, anywhere on this server.
	    //

	    String errMsg = lStrings.getString("http.method_not_implemented");
	    Object[] errArgs = new Object[1];
	    errArgs[0] = method;
	    errMsg = MessageFormat.format(errMsg, errArgs);
	    
	    resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
	}
    }

HttpServlet Example

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class HttpServletExample extends HttpServlet{
 
	public void doGet(HttpServletRequest request, HttpServletResponse respon        se) throws IOException
        {
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<body>");
		out.println("<h2>Http Servlet Example!!!</h2>");
        	out.println("</body>");
		out.println("</html>");	
	}
}

Leave a Reply

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