Home arrow Java arrow Page 5 - An Introduction to Java Servlets

An Introduction to Java Servlets

Java servlets are making headlines these days, claiming to solve many of the problems associated with CGI and proprietary server API's. In this article Nakul describes the overall servlet architecture and what you need to develop a web application using servlets. He uses several coding examples to show you how to use the servlet API and compares it with CGI and proprietary server API's.

Author Info:
By: Nakul Goyal
Rating: 4 stars4 stars4 stars4 stars4 stars / 53
March 11, 2002
  1. · An Introduction to Java Servlets
  2. · The dark ages
  3. · Servlets to the rescue!
  4. · The servlet runtime environment
  5. · Servlet interface and life cycle
  6. · Request and response objects
  7. · Persistent and Shared Data
  8. · ServletContext attributes
  9. · Request attributes and resources
  10. · Conclusion

print this article

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 {


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 {



blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials