An Introduction to Java Servlets - Servlet interface and life cycle
(Page 5 of 10 )
Let's implement our first servlet. A servlet is a Java class that implements the servlet interface. This interface has three methods that define the servlet's life cycle:
- public void init(ServletConfig config) throws ServletException: This method is called once when the servlet is loaded into the servlet engine, before the servlet is asked to process its first request.
- public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException: This method is called to process a request. It can be called zero, one or many times until the servlet is unloaded. Multiple threads (one per request) can execute this method in parallel, so it's thread safe.
- public void destroy(): This method is called once just before the servlet is unloaded and taken out of service.
The init method has a ServletConfig attribute. The servlet can read its initialization arguments through the ServletConfig object. How the initialization arguments are set is servlet engine dependent, but they are usually defined in a configuration file.
A typical example of an initialization argument is a database identifier. A servlet can read this argument from the ServletConfig at initialization and then use it later to open a connection to the database during processing of a request:
...
private String databaseURL;
public void init(ServletConfig config) throws ServletException {
super.init(config);
databaseURL = config.getInitParameter("database");
}The servlet API is structured so that servlets implementing a protocol other than HTTP are also possible. The javax.servlet package contains interfaces and classes that are intended to be protocol independent and the javax.servlet.http package contains HTTP specific interfaces and classes. Since this is just an introduction to servlets, I will ignore this distinction here and focus on HTTP servlets. Our first servlet, named ReqInfoServlet, will therefore extend a class named HttpServlet. HttpServlet is part of the JSDK and implements the Servlet interface plus a number of other convenience methods. We define our class like this:
import javax.servlet.*;
import javax.servlet.http.*;
public class ReqInfoServlet extends HttpServlet {
...
}The important set of methods in HttpServlet are those that specialize the service method in the servlet interface. The implementation of service in HttpServlet looks at the type of request it's asked to handle (GET, POST, HEAD, etc.) and calls a specific method for each type. By doing this, the servlet developer is relieved from handling the details about obscure requests like HEAD, TRACE and OPTIONS, and can focus on taking care of the more common request types such as GET and POST. In this first example we will only implement the doGet method.
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
...
}Next: Request and response objects >>
More Java Articles
More By Nakul Goyal