Programmer's Guide to Servlets in Enterprise Server 4.0
Table of Contents | Previous | Next | Index

Programmer's Guide to Servlets in Enterprise Server 4.0


Appendix A
Session Managers

Session objects maintain state and user identity across multiple page requests over the normally stateless HTTP protocol. A session persists for a specified time period, across more than one connection or page request from the user. A session usually corresponds to one user, who may visit a site many times. The server can maintain a session either by using cookies or by rewriting URLs. Servlets can access the session objects to retrieve state information about the session.

This appendix has the following sections:

Session Overview

An HTTP session represents the server's view of the session. The server considers a session new under these conditions:

A session manager automatically creates new session objects whenever a new session starts. In some circumstances, clients will not join the session, for example, if the session manager uses cookies and the client does not accept cookies.

Enterprise Server 4.0 comes with two session managers for creating and managing sessions:

Enterprise Server 4.0 also allows you to develop your own session managers and load them into the server. The build includes the source code for SimpleSessionManager and the session objects it manages, SimpleSession. The source code for these classes are provided as a starting point for you to define your own session managers if desired. These Java files are in the directory server-root/plugins/samples/servlets/sessions/SimpleSession.

Specifying a Session Manager

By default, if the Enterprise Server starts in single process mode, it uses SimpleSessionManager as the session manager for servlets. If it starts in multi-process mode, it uses MMapSessionManager. For more information about single process mode versus multi processes mode, see Chapter 7, "Configuring Server Preferences" in the Enterprise Server 4.0 Administrator's Guide.

You can change the session manager in either of the following ways:

SimpleSessionManager

The SimpleSessionManager works only in single process mode. It is loaded by default if the Enterprise Server starts in single-process mode when a SessionManager is not specified in the servlets.properties configuration file. These sessions are not persistent, that is, all sessions are lost when the server is stopped.

Parameters

The SimpleSessionManager class takes the following parameters:

Enabling SimpleSessionManager

To enable the Enterprise Server to use SimpleSessionManager do either of the following:

Source Code for SimpleSessionManager

The SimpleSessionManager creates a SimpleSession object for each session. The source files for SimpleSessionManager.java and SimpleSession.java are in the directory server-root/plugins/samples/servlets/sessions/SimpleSession.

The source code for SimpleSessionManager.java and SimpleSession.java are provided so you can use them as the starting point for defining your own session managers and session objects. These files are very well commented.

SimpleSessionManager extends NSHttpSessionManager. The class file for NSHttpSessionManager is in the JAR file NSServletLayer.jar in the directory server_root/plugins/jar. SimpleSessionManager implements all the methods in NSHttpSessionManager that need to be implemented, so you can use SimpleSessionManager as an example of how to extend NSHttpSessionManager. When compiling your subclass of SimpleSessionManager or NSHttpSessionManager, be sure that the JAR file NSServletLayer.jar is in your compiler's class path.

MMapSessionManager

This is a persistent memory map file-based session manager that works in both single process as well as multi-process mode. It can be used for inter-process communication. It is loaded by default if the Enterprise Server starts in multi-process mode when a session manager is not specified in the servlets.properties configuration file.

Parameters

MMapSessionManager takes the following parameters:

Enabling MMapSessionManager

To enable Enterprise Server to use MMapSessionManager do either of the following:

This session manager can only store objects that implement java.io.Serializable.

How Do Servlets Access Session Data?

To access the state information stored in a session object, your servlet can create a new session as follows:

// request is an HttpServletRequest that is passed to the servlet
SessionClass session = request.getSession(true);
The servlet can call any of the public methods in javax.servlet.http.HttpSession on the session object. These methods include (amongst others):

getCreationTime
getId
getLastAccessedTime
getMaxInactiveInterval
getValue
For more information about the classes HttpServletRequest and HttpSession, see Sun Microsystem's API Servlets Documentation at:

http://www.javasoft.com/products/servlet/2.1/html/api-reference.fm.html

Table of Contents | Previous | Next | Index

Last Updated: 08/12/99 12:39:28

Copyright © 1999 Netscape Communications Corp. All rights reserved.