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

Trail: Servlets
Lesson: Servlet Communication

Sharing Resources Among Servlets

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.

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.


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

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.