How to Implement Servlets

The Servlet Interface :

      The Servlet interface is the central abstraction of the Java Servlet API. All servlets implement this interface either directly, or more commonly, by extending a class that implements the interface. The two classes in the Java Servlet API that implement the Servlet interface are GenericServlet and HttpServlet. For most purposes, Developers will extend HttpServlet to implement their servlets.

Request Handling Methods :

      The basic Servlet interface defines a service method for handling client requests. This method is called for each request that the servlet container routes to an instance of a servlet. The Web container handles concurrent requests to the same servlet by concurrent execution of the service method on different threads.

HTTP Specific Request Handling Methods :

      The client can make the request to the web server using HTTP protocol. The HttpServlet abstract subclass adds additional methods beyond the basic Servlet interface that are automatically called by the service method in the HttpServlet class to aid in processing of these HTTP-based requests. These methods are :

1. doGet for handling HTTP GET requests.
2. doPost for handling HTTP POST requests.
3. doPut for handling HTTP PUT requests.
4. doDelete for handling HTTP DELETE requests.
5. doHead for handling HTTP HEAD requests.
6. doOptions for handling HTTP OPTIONS requests.
7. doTrace for handling HTTP TRACE requests.

Typically when developing HTTP-based servlets, a Servlet Developer will only concern himself with the doGet and doPost methods. The other methods are considered to be methods for use by programmers very familiar with HTTP programming.

The doPut and doDelete methods allow Servlet Developers to support HTTP/ 1.1 clients that employ these features. The doHead method in HttpServlet is a specialized form of the doGet method that returns only the headers produced by the doGet method. The doTrace method generates a response containing all instances of the headers sent in the TRACE request.

Conditional GET Support :

The HttpServlet interface defines the getLastModified method to support conditional GET operations. A conditional GET operation requests a resource be sent only if it has been modified since a specified time. In appropriate situations, implementation of this method may aid efficient utilization of network resources.

Number of Instances :

The servlet declaration which is either via the annotation or part of the deployment descriptor of the Web application containing the servlet, controls how the servlet container provides instances of the servlet.

For a servlet not hosted in a distributed environment, the servlet container must use only one instance per servlet declaration. However, for a servlet implementing the SingleThreadModel interface, the servlet container may instantiate multiple instances to handle a heavy request load and serialize requests to a particular instance.

In the case where a servlet was deployed as part of an application marked in the deployment descriptor as distributable, a container may have only one instance per servlet declaration per Java Virtual Machine. However, if the servlet in a distributable application implements the SingleThreadModel interface, the container may instantiate multiple instances of that servlet in each JVM of the container.

A Simple Servlet program for handling HTTP Requests: 

       In order to handle HTTP Requests , our servlet needs to extend HttpServlet class. Here is the code.  

          import javax.servlet.*;
          import javax.servlet.http.*;
public class LandSelectServlet extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException {

   String language = req.getParameter("language");
   PrintWriter out = res.getWriter();
   out.println("<b> Done !!!");
   out.println("You have chosen " + language);