Servlet Methods

Servlet Methods section describe you about the methods of Servlet interface.

Servlet Methods

In this section we will read about the various methods of javax.servlet.Servlet interface.

javax.servlet.Servlet interface declares some methods that are used to initialize a Servlet, handles the request received and generated response, and to destroy a Servlet and its resources. Generally, the Servlet methods are called life-cycle methods of Servlet.

Following are the Servlet methods :

  • void init() : Servlet container calls this method to specify that the Servlet has to be placed for service. In the whole life-cycle of Servlet the init() method is called only once. Syntax of init() method
    public void init(ServletConfig config) throws ServletException
    A Servlet can't be started by the following two reasons :
    • If, the init() method doesn't return within a specified time specified by the server
    • If, the init() method throws ServletException.

  • void service() : Once, the Servlet is started i.e. init() method is return within the specified server time and the init() method doesn't throws ServletException then a Servlet is ready to provide the services. The service method is based on the HTTP request and response. Service method handles the request and response using two objects javax.servlet.ServletRequest and javax.servlet.ServletResponse. Syntax of service() method
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
    The HTTP based service methods can be based on HTTP request i.e.
    • doGet() for GET request
    • doPost() for POST request
    • doPut() for PUT request
    • doDelete() for DELETE request
    • doHead() for HEAD request
    • doOptions() for OPTIONS request
    • doTrace() for TRACE request

  • void destroy() : Servlet container calls this method to specify that the Servlet has to be taken out of service. In the whole lif-cycle of the Servlet destroy() method is called only once. Syntax of destroy() method
    public void destroy()
  • String getServletInfo() : This method is used to find out the information of a Servlet like, version, author etc. Syntax of getServletInfo()
     public String getServletInfo()
  • ServletConfig getServletConfig() : This method is used to get the ServletConfig object. Syntax of getServletConfig()
    public ServletConfig getServletConfig() 


Here I am giving a simple example which will demonstrate you about how to use Servlet methods mentioned above. In this example we will create a Servlet where will use all the Servlet methods.

Example of the Servlet having all parts of its lifecycle:



import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletLifeCycleExample extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public void init(ServletConfig config) throws ServletException
    	System.out.println("init() method is called ");
   	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   	    PrintWriter out = response.getWriter();   	    
   	    out.println("service() method called");

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	public void destroy()
		System.out.println("Servlet is destroying....");


Servlet mapping for the above example


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="3.0">
Share on Google+Share on Google+

Servlet Methods

Posted on: June 29, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Servlet Methods  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image