ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzatz_5.4.0.1/51/program/sesdev.htm

77 lines
4.6 KiB
HTML
Raw Permalink Normal View History

2024-04-02 14:02:31 +00:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<LINK rel="stylesheet" type="text/css" href="../../../rzahg/ic.css">
<title>Session programming model and environment</title>
</head>
<BODY>
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h4><a name="sesdev"></a>Session programming model and environment</h4>
<p>The session lifecycle, from creation to completion, is as follows:</p>
<ol>
<li>Get the HttpSession object.</li>
<li>Store and retrieve user-defined data in the session.</li>
<li>(Optional) Output an HTML response page containing data from the HttpSession object.</li>
<li>(Optional) Notify Listeners.</li>
<li>End the session.</li>
</ol>
<p>The steps are described in detail below. This information, combined with the coding example, provides a programming model for implementing session in servlets. For more information, see <a href="sessamp.htm">Example: SessionSample.java</a>.</p>
<p>For more information, see the API documentation: <a href="apidocs/ae/com/ibm/websphere/servlet/session/package-summary.html">Package com.ibm.websphere.servlet.session</a>. <img src="api.gif" width="18" height="15" align="bottom" alt="Go to API documentation"></p>
<p><strong>The lifecycle in detail</strong></p>
<ol>
<li><p><strong>Get the HttpSession object.</strong>
<br>To obtain a session, use the getSession() method of the javax.servlet.http.HttpServletRequest object in the Java Servlet 2.3 API.</p>
<p>When you first obtain the HttpSession object, the Session Manager uses one of these ways to establish tracking of the session:</p>
<ul>
<li>cookies</li>
<li>URL rewriting</li>
<li>SSL information</li>
</ul>
<p>See <a href="sesdeci.htm">Deciding between session tracking approaches</a> for more information.</p>
<p>Assume the Session Manager uses cookies. In such a case, the Session Manager creates a unique session ID and typically sends it back to the browser as a cookie. Each subsequent request from this user (at the same browser) passes the cookie containing the session ID, and the Session Manager uses this to find the user's existing HttpSession object.</p>
<p>In Step 1 of the code sample, the Boolean(create) is set to true so that the HttpSession is created if it does not already exist. (With the Servlet 2.3 API, the javax.servlet.http.HttpServletRequest.getSession() method with no boolean defaults to true and creates a session if one does not already exist for this user.)</p></li>
<li><p><strong>Store and retrieve user-defined data in the session.</strong>
<br>After a session is established, you can add and retrieve user-defined data to the session. The HttpSession object has methods similar to those in java.util.Dictionary for adding, retrieving, and removing arbitrary Java objects.</p>
<p>In Step 2 of the code sample, the servlet reads an integer object from the HttpSession, increments it, and writes it back. You can use any name to identify values in the HttpSession object. The code sample uses the name sessiontest.counter.</p>
<p>Because the HttpSession object is shared among servlets that the user might access, consider adopting a site-wide naming convention to avoid conflicts.</p></li>
<li><p><strong>(Optional) Output an HTML response page containing data from the HttpSession object.</strong>
<br>To provide feedback to the user that an action has taken place during the session, you may wish to pass HTML code to the client browser that indicates that an action has occurred.</p>
<p>For example, in step 3 of the code sample the servlet generates a Web page that is returned to the user and displays the value of the sessiontest.counter each time the user visits that Web page during the session.</p></li>
<li><p><strong>(Optional) Notify Listeners.</strong>
<br>Objects stored in a session that implement the javax.servlet.http.HttpSessionBindingListener interface are notified when the session is preparing to end, that is, about to be invalidated. This notice enables you to perform post-session processing.</p></li>
<li><p><strong>End the session.</strong>
<br>You can end a session in one of these ways:</p>
<ul>
<li>Automatically with the Session Manager, if a session has been inactive for a specified time. The administrative clients provide a way to specify the amount of time after which to invalidate a session.</li>
<li>By coding the servlet to call the invalidate() method on the session object.</li>
</ul></li>
</ol>
</body>
</html>