The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search

Trail: Servlets
Lesson: Servlet Communication

Sharing Resources Among Servlets (JSDK 2.1)

Servlets running in the same server sometimes share resources. This is especially true of servlets that combine to form a single application, as the Duke's Bookstore servlets do. Servlets in the same server can share resources using the ServletContext interface's methods for manipulating attributes: setAttribute, getAttribute, and removeAttribute.

Note that using attributes is one way to remove the deprecated method, getServlet from pre-JSDK2.1 servlets.

Naming Conventions for Attributes

All servlets in a context share the attributes stored with the ServletContext interface. To avoid collisions of attribute names, name attributes using the same conventions as package names. For example, the Duke's Bookstore servlets share an attribute called examples.bookstore.database.BookDBFrontEnd. Names beginning with the prefixes java.*, javax.*, and sun.* are reserved.

Setting an Attribute

Servlets set attributes using the ServletContext.setAttribute method; they typically do so during initialization. When you have multiple servlets sharing an attribute, you might have each one try to initialize the attribute. If so, each servlet should check for the attribute value, and only set the attribute if another servlet has not already done so.

The following example shows the init method of the CatalogServlet trying to set the shared attribute for Duke's Bookstore:

public class CatalogServlet extends HttpServlet { 

    public void init() throws ServletException {
        BookDBFrontEnd bookDBFrontEnd = ...

        if (bookDBFrontEnd == null) {
            getServletContext().setAttribute(
                "examples.bookstore.database.BookDBFrontEnd",
                BookDBFrontEnd.instance());
        }
    }
...
}

Once a servlet sets an attribute, any servlet in the same context can retrieve its value, reset its value, or remove the attribute.

getAttribute

Getting the value of the attribute is as simple as making a call to the ServletContext.getAttribute method. The following example shows the CatalogServlet getting the value of an attribute during initialization:

public class CatalogServlet extends HttpServlet { 

    public void init() throws ServletException {
        BookDBFrontEnd bookDBFrontEnd =
            (BookDBFrontEnd)getServletContext().getAttribute(
                "examples.bookstore.database.BookDBFrontEnd");

        if (bookDBFrontEnd == null) {
            getServletContext().setAttribute(
                "examples.bookstore.database.BookDBFrontEnd",
                BookDBFrontEnd.instance());
        }
    }
...
}

removeAttribute

Any servlet can remove an attribute from a ServletContext object. Because attributes are shared, however, you must take care that you do not delete an attribute that another servlet might still use. Duke's Bookstore does not use the removeAttribute method.

Converting Servlets from JSDK2.0

Note for converting servlets written with the JSDK2.0: The methods for managing attributes can be useful if you are removing calls to the getServlet method. Consider these methods for servlets that called the getServlet method and then called public methods other than the service method.

Removing calls to getServlet from these servlets involves:

  1. Changing the servlet gotten with the call to getServlet to a non servlet class.
  2. Naming an attribute that will hold an instance of the new class for use by the servlets that called the getServlet method.
  3. Replacing the call to the getServlet method with calls that Get the value of the previously named attribute
  4. Changing calls to methods of the Servlet object to calls to methods of the instance returned from the attribute.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search