Skip to main content

Httpservlet and Genericservlet

HttpServlet vs GenericServlet

There are two ways to create servlet

  1.  By extending Httpservlet
  2.  By extending Genericservlet

servlet-hierarchy

GenericServlet

  • GenericServlet class is an Abstract class and sub class of GenricServlet is called Servlet.
  • It can handle any type of Protocol including HTTP and HTTPS. so It is called Protocol independent servlet.
  • GenericServlet which is part of Servlet API, as only one method service() which is declared as Abstract hence genericServlet is an Abstract class.

Syntax:

public abstract void service(ServletRequest req,ServletResponse res)
           Throws Servlet Exception,IOException)
Creating servlet by extending GenerivServlet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.spy.javaforlearn.servlets;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
 
public class MyGenericServlet 
extends GenericServlet 
{
	@Override
	public void service(ServletRequest req, 
			   ServletResponse resp)
	throws ServletException, IOException 
	{
 
		resp.setContentType("text/html");
		PrintWriter out = resp.getWriter();
		out.println("Inside MyGenericServlet ....");
 
 
	}//End of service
}//End of Class

HttpServlet

  • HttpServlet is an abstract class and sub clas of HttpServlet called as Servlet and It can handle only HTTP and HTTPS protocol
  • In other words it becomes Protocol Protocol dependent servlet
  • It is an abstract class but none of the method in this class is declared as abstract.
  • A sub class of HTTP servlet can override any of the below service method.
public void service(ServletRequest req,ServletResponse res)
           Throws Servlet Exception,IOException)
public void service(HttpServletRequest req,HttpServletResponse res)
           Throws Servlet Exception,IOException)
protected void doXXX(HttpServletRequest req,HttpServletResponse res)

Note: We should not override the the first two version of service method our job is to override one/more doXXX() method.if we won’t override DoXXX() method then default implementation from HttpServlet is invoked which return 405 error response.

Why HttpServlet is an abstract class?

Httpservlet does not have any abstract method but being abstract we are forced to subclass it, so it force to implement our own servlet class.
Creating servlet by extending HttpServlet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.spy.javaforlearn.servlets;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
 
public class Myservlet extends HttpServlet 
{
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException 
	{
 
		response.setContentType("text/html");
 
		PrintWriter out=response.getWriter();
 
		out.print("inside get method");
 
	}
 
 
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException 
	{
 
		//post method
 
	}
 
}