Database Reference
In-Depth Information
Some session-related operations shown in sess_track.jsp can be done using tags from
the JSTL tag library, which provides a sessionScope variable for accessing the implicit
JSP session object. The following script, sess_track2.jsp , uses that variable. One differ‐
ence in approach is that sess_track.jsp terminates the session by calling session.inva
lidate() , but the sessionScope variable provides no access to that method. Instead,
sess_track2.jsp terminates the session by deleting the session contents, causing the ses‐
sion to restart with the next client request:
<%- -
sess_track2 . jsp : session request counting / timestamping demonstration
-- %>
<%@ page import = "java.util.*" %>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<c:if test= "${empty sessionScope.count}" >
<c:set var= "count" scope= "session" value= "0" />
</c:if>
<c:set var= "count" scope= "session" value= "${sessionScope.count+1}" />
<%
ArrayList timestamp = ( ArrayList ) session . getAttribute ( "timestamp" );
if ( timestamp == null )
timestamp = new ArrayList ();
// add current timestamp to timestamp array, store result in session
timestamp . add ( new Date ());
session . setAttribute ( "timestamp" , timestamp );
%>
<html>
<head><title> JSP Session Tracker </title></head>
<body>
<p> This session has been active for
<c:out value= "${sessionScope.count}" />
requests. </p>
<p> The requests occurred at these times: </p>
<ol>
<c:forEach items= "${sessionScope.timestamp}" var= "t" >
<li><c:out value= "${t}" /></li>
</c:forEach>
</ol>
<p> Reload page to send next request. </p>
<%- - has session limit of 10 requests been reached ? -- %>
<c:if test= "${sessionScope.count ge 10}" >
<c:remove var= "count" scope= "session" />
<c:remove var= "timestamp" scope= "session" />
</c:if>
Search WWH ::




Custom Search