Servlet Methods

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

Servlet Methods

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() 
    

Example

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:

package roseindia.net;

import java.io.IOException;
import java.io.PrintWriter;

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 {
   		response.setContentType("text/plain");
   	    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

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>servletLifeCycleExample</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file> 
</welcome-file-list>
<servlet>
<servlet-name>ServletLifeCycleExample</servlet-name>
<servlet-class>roseindia.net.ServletLifeCycleExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletLifeCycleExample</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
</web-app>