Database Reference
In-Depth Information
Object obj = session . getAttribute ( "count" );
if ( obj == null )
count = 0 ;
else
count = Integer . parseInt ( obj . toString ());
ArrayList timestamp = ( ArrayList ) session . getAttribute ( "timestamp" );
if ( timestamp == null )
timestamp = new ArrayList ();
// increment counter, add current timestamp to timestamp array
count = count + 1 ;
timestamp . add ( new Date ());
if ( count < 10 ) // save updated values in session object
{
session . setAttribute ( "count" , String . valueOf ( count ));
session . setAttribute ( "timestamp" , timestamp );
}
else // restart session after 10 requests
{
session . invalidate ();
}
%>
<html>
<head><title> JSP Session Tracker </title></head>
<body>
<p> This session has been active for <%= count %> requests. </p>
<p> The requests occurred at these times: </p>
<ol>
<%
for ( int i = 0 ; i < timestamp . size (); i ++)
out . println ( "<li>" + timestamp . get ( i ) + "</li>" );
%>
</ol>
<p> Reload page to send next request. </p>
</body>
</html>
sess_track.jsp is included in the mcb application (see Recipe 18.3 ). Invoke it from your
browser and reload it a few times to see how the display changes.
The session.setAttribute() method used in sess_track.jsp places information into
the session so that it can be found by later invocations of the script. But session attributes
also can be shared with other scripts in the same application context, which have access
to the same information. You'll see this with our next version of the script, which when
you invoke it accesses the same session information as sess_track.jsp .
Search WWH ::




Custom Search