Recipe 11.6 Tracking Session Activity in JSPs


Problem

You want to find out a session's creation time and last-accessed time using JSPs.

Solution

Use the JSTL to get access to the JSP's associated HttpSession object. Then call the HttpSession.getCreationTime( ) and HttpSession.getLastAccessedTime( ) methods on that object.

Discussion

It is very easy to keep track of session activity in a JSP; you just use slightly different methods and tools compared to those used with a servlet. Example 11-8 uses the out custom action from the JSTL 1.0 to display information about the current session. Chapter 24 describes the JSTL and its associated EL in more detail.

Example 11-8. Tracking sessions using the JSTL
 <%@page contentType="text/html"%>  <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>  <html>   <head><title>View Session JSP </title></head>   <body>     <h2>Session Info From A JSP</h2>  The session id: <c:out value="${pageContext.session.id}"/><br><br>     The session creation time as a long value:        <c:out value="${pageContext.session.creationTime}"/><br><br>     The last accessed time as a long value:        <c:out value="${pageContext.session.lastAccessedTime}"/><br><br>  </body> </html> 

This JSP uses a taglib directive to make the custom actions that are part of the core tag library available. By convention, the uri attribute for the core tags is http://java.sun.com/jstl/core , and the prefix is c (you can create your own prefix in the taglib directive). With JSTL 1.1, the uri value is http://java.sun.com/jsp/jstl/core . The JSP then uses the out tag from the JSTL's core tag library to display the current session ID (the return value of HttpSession.getId( ) ), the session's creation time as a long type, and the session's last-accessed time. Figure 11-4 shows a browser display of these values.

Figure 11-4. Showing session info in a JSP
figs/jsjc_1104.gif

The out element writes the value of its value attribute to the JSP's response stream. However, it is the EL that does the fetching of the value. For example, the following EL expression gets the value of the session's creation time:

 ${pageContext.session.creationTime} 

The pageContext reference is one of the implicit objects that can be accessed from the EL. This is equivalent to the implicit JSP scripting object of the same name .


The way the creation time is accessed is different than a method call; you use the dot (.) operator to get the pageContext 's session property, then in turn use the dot operator to access the session object's creationTime property. So the whole phrase looks like this:

 pageContext.session.creationTime. 

Finally, in the EL, all variable and property values are dereferenced (to get their values) by bracketing them in ${ } characters .

The JSP gets the other session values in the same way. For example, the session's last-accessed time (the long type return value from the method HttpSession.getLastAccessedTime( ) ) is returned using this syntax:

 ${pageContext.session.lastAccessedTime} 

Example 11-8 displays the last-accessed time for a session as a large, unfriendly number. Naturally, this value is more understandable displayed as a date. Example 11-9 shows how to use the JSTL's custom formatting actions to format a date string.

Example 11-9. Formatting the session creation time and last-accessed time with the JSTL
 <%@page contentType="text/html"%>  <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>  <html>   <head><title>View Session JSP </title></head>   <body>     <h2>Session Info From A JSP</h2>     The session ID: <c:out value="${pageContext.session.id}"/>     <h3>Session date values formatted as Dates</h3>  <jsp:useBean id="timeValues" class="java.util.Date"/>     <c:set target="${timeValues}" value=       "${pageContext.session.creationTime}" property="time"/>     The creation time: <fmt:formatDate value="${timeValues}" type="both"             dateStyle="medium" /><br><br>     <c:set target="${timeValues}" value=     "${pageContext.session.lastAccessedTime}" property="time"/>     The last accessed time:  <fmt:formatDate value="${timeValues}" type=       "both" dateStyle="short" />  </body> </html> 

Figure 11-5 shows the browser display for this JSP.

Figure 11-5. Session date/times formatted using the JSTL
figs/jsjc_1105.gif

This JSP takes two date- related session values: the date/time when the session was created and the last date/time when a request associated with this session was made to the web application. It displays their values in the browser. As mentioned previously, these values are returned from the HttpSession object as long Java types. You have to create a Date object with its time property set to these long values. Then use the JSTL formatting custom actions to create readable Strings from the dates. First, make the formatting tag library available to the JSP with this taglib directive:

 <%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %> 

The java.util.Date object that will be used to create dates out of long values is generated using a JSP standard action called jsp:useBean . Here is the example's syntax:

 <jsp:useBean id="timeValues" class="java.util.Date"/> 

This line creates a new Date object and stores the object in a variable called timeValues , making it available through the EL with the syntax ${timeValues} . The JSP then uses the set custom action to set a time property in the Date object:

 <c:set target="${timeValues}" value="${pageContext.session.creationTime}"         property="time"/> 

The value of set 's target attribute is the JavaBean whose property you are setting. The property name is specified by the set element's property attribute. The value this expression sets the time property to is the long type returned from this JSTL expression:

 ${pageContext.session.creationTime} 

In other words, using the custom action this way is the equivalent of calling the java.util.Date.setTime(long secs) method on the timeValues Date object. This time value is actually set and displayed twice, to represent the creation time and last-accessed time of the session. Example 11-10 is the code chunk that does the setting and displaying, including the fmt:formatDate custom action.

Example 11-10. Displaying a session's creation time and last-accessed time
 <c:set target="${timeValues}" value="${pageContext.session.creationTime}"         property="time"/> The creation time: <fmt:formatDate value="${timeValues}" type="both"         dateStyle="medium" /><br><br> <c:set target="${timeValues}"value=   "${pageContext.session.lastAccessedTime}" property="time"/> The last accessed time:  <fmt:formatDate value="${timeValues}" type="both"         dateStyle="short" /> 

The formatDate element is one of the JSTL's formatting actions, which are described in Chapter 24. The way the formatDate action works in this example is that the following code is replaced by the formatted date value, as in "Jan 21, 2003 1:57:39 PM":

 <fmt:formatDate value="${timeValues}" type="both" dateStyle="short" /> 

In order to display their differences, Example 11-10 gives two different values ( medium and short ) for the dateStyle attribute.

See Also

Recipe 11.4 on checking the validity of a session; Chapter 1 on web.xml ; Chapter 7 of the Servlet v2.3 and 2.4 specifications on sessions; the javax.servlet.http.HttpSession API at http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpSession.html; the session-tracking sections of Java Servlet Programming by Jason Hunter (O'Reilly) and JavaServer Pages by Hans Bergsten (O'Reilly).



Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net