HttpServlet class

HttpServlet is an abstract class present in javax.servlet.http package. This class is a subclass of GenericServlet class and used widely to create a servlet. Although HttpServlet extends GenericServlet, still there are much differences between them. Differences between GenericServlet and HttpServlet

           GenericServlet           HttpServlet
This class is present in javax.servlet package. This class is present in javax.servlet.http package.
This class is protocol independent i.e it can handle any type of request. This class is protocol dependent i.e it can handle only http request.
This class must override service() method. This class must override atleast one of the provided method such as – doGet(), doPost(), doPut(), doDelete()
Using this class we cannot handle cookies. This class is capable to handle cookies.
HttpServlet methods Here are some generally used HttpServlet methods:
                 Methods                 Description
protected void doGet(HttpServletRequest req, HttpServletResponse res) This is one of the widely used method called by the server to handle GET request.
protected void doPost(HttpServletRequest req, HttpServletResponse res) This is also one of the most used method called by the server to handle POST request.
protected void doDelete(HttpServletRequest req, HttpServletResponse res) This method allows servlet to perform delete operations on the server.
protected void doHead(HttpServletRequest req, HttpServletResponse res) This method is used to enhance the performance by putting the responses in the header instead of responses body.
protected void doTrace(HttpServletRequest req, HttpServletResponse res) Server called this method to handle TRACE request.
protected void doPut(HttpServletRequest req, HttpServletResponse res) This method can be used to put the documents on the server. Thus, it handles the PUT request.
protected void doOptions(HttpServletRequest req, HttpServletResponse res) This method is used to determine the HTTP method supported by the server.
Difference between doGet() and doPost() method
                   doGet()                 doPost()
In this method, data is transferred in headers. In this method data is transferred in body.
Less data is transferred. More data is transferred
This method provides less security as data is exposed on url bar. This method provides more security as data is not exposed on url bar.
We can perform task without using external HTML. We cannot perform task without using external HTML.
This method is generally used to retrieve information from server. This method is generally used to send information to server.
HttpServlet program Let’s see a simple example of HttpServlet class. HttpServletExample.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HttpServletExample extends HttpServlet
{
	public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
	{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
pw.println("<html><body>");
pw.println("<h1>doPost() method is invoked</h1>");
pw.println("</body></html>");
	}
	public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
	{		
		res.setContentType("text/html");
		PrintWriter pw=res.getWriter();
		pw.println("<html><body>");
		pw.println("<h1>doGet() method is invoked</h1>");
		pw.println("</body></html>");		
	}
}
Output: Notice here doPost() method is not invoked. If we have to invoke this method then call it in doGet() method. HttpServletExample.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class  HttpServletExample extends HttpServlet
{
	public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
	{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
pw.println("<html><body>");
pw.println("<h1>doPost() method is invoked</h1>");
pw.println("</body></html>");
	}
	public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
	{
		doPost(req,res);
		res.setContentType("text/html");
		PrintWriter pw=res.getWriter();
		pw.println("<html><body>");
		pw.println("<h1>doGet() method is invoked</h1>");
		pw.println("</body></html>");		
	}
}
Output: Now you can see doPost() method is also invoked but not directly. Externally we use doGet() method to call it.