How a Servlet is defined?

In this post, you will learn:

  • How to implement Servlet interface directly and indirectly?
  • What is the role of HttpServlet class?
  • What are the different request types of HTTP?

A servlet class can be defined by directly implementing the Servlet interface or by extending a helper class javax.servlet.GenericServlet provided by servlet API. It is an abstract class, which implements Servlet interface and defines all its methods except service() i.e. In its sub class, an application programmer need to define only the service method. Following diagram, describes both these approaches:

In web applications usually requests are submitted using HTTP protocol which supports following eight type of requests:

[one_half]
  • Get
  • Post
  • Put
  • Delete
[/one_half] [one_half_last]
  • Head
  • Trace
  • Options
  • Connect

[/one_half_last]

Out of these, Get & Post are used for requesting contents from the server and other request types are used for connection setup and management. Servlet interface provides only one method, service() for request processing i.e. there is mismatch between the protocol and the request processing mechanism. To facilitate processing of HTTP requests, servlet API provides HTTP specific classes and interfaces.

javax.servlet.http package contains HTTP specific classes and interfaces of servlet API. Most commonly used components of this package are:

  1. HttpServletRequest: It extends ServletRequest and adds HTTP specific methods.

  2. HttpServletResponse: It extends ServletResponse and adds HTTP specific methods.

  3. HttpServlet: It is an abstract class, it extends GenericServlet and provides methods for processing each HTTP request type. Most commonly used methods of this class are doGet() and doPost(). These are used to process HTTP Get & Post request respectively.

Syntax:

public void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;

public void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;

By extending HttpServlet class and overridding doGet() & doPost() methods, in a user defined class HTTP Get & Post requests can be processed as described by the following diagram.

A Servlet which procsses HTTP Get & Post request will have following structure:

package pkgName;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class ServletName extends HttpServletRequest
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//code to process get request
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//code to process post request
}
} 

Its time to recap the main concepts:

  1. When the Servlet interface is directly implemented, all the five methods need to be defined.
  2. When GenericServlet is extended, only the service() method need to be defined. Other methods are inherited.
  3. Servlet interface provides only one method service() for request processing whereas eight types of requests can be sent using HTTP.
  4. HttpServlet class provides methods to process each HTTP request type.
  5. HTTP get & post requests are used for requesting contents from the server.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

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