MSc-IT Study Material
June 2010 Edition

Computer Science Department, University of Cape Town

The Servlet packages and classes

There are two main packages which this chapter makes use of: javax.servlet and javax.servlet.http.

javax.servlet

The javax.servlet package provides all of the base classes and interfaces defining both what a Servlet is, and how it interacts with the Web server that is running it. Of special note are:

Interfaces

  • Servlet – This defines all the methods that a Servlet must implement. It includes methods to initialize the destroy the Servlet, and a general method (service()) which handles all requests made to it. Note that classes which implement the Servlet interface need not only handle HTTP requests: they can handle any other request made to the server over other protocols. Because of this, the Servlet interface does not have the doGet() or doPost() methods, but only the general service() method. This interface also defines the init() and destroy() methods, which can be overridden to perform initialise resources used by the Servlet, and the release them when the Servlet is destroyed.

  • ServletRequest – An interface which defines the methods for all objects encapsulating information about requests made to the server.

  • ServletResponse – An interface defining the methods for all objects which return a response from the server.

  • ServletConfig – Defines an interface used to gain access to configuration parameters which are passed to the Servlet during initialisation.

Classes:

  • GenericServlet – This is a generic class implementing Servlet. If you wish to write Servlet's for protocols other than HTTP, the easiest way of doing so is to extend GenericServlet rather than by directly implementing the Servlet interface.

  • ServletException – An exception which can be thrown when the Servlet encounters a problem of some kind.

javax.servlet.http

The javax.servlet.http package provides classes specific to handling HTTP requests. It provides the HttpServlet class used in this chapter, which implements the appropriate interfaces from javax.servlet.

Interfaces:

  • HttpServletRequest – An extension to the ServletRequest interface for features specific to HTTP.

  • HttpServletResponse – An extension to the ServletResponse interface for features specific to HTTP.

  • HttpSession – Provides access to the session tracking API.

Classes:

  • HttpServlet – An abstract class providing functionality to implement HTTP requests. Note that the service() method defined in the Servlet interface will now call doGet() and doPost(), which can each be implemented to provide behaviour to the Servlet.

  • Cookie – The cookie class, which provides an interface for storing small portions of data on the user's computer.

  • HttpServletRequestWrapper and HttpServletResponseWrapper – Provides an implementation of the HttpServletResponse and HttpServletRequest interfaces.