- Using Hidden Controls
- The Cookie Class
- The HttpServletResponse Interface
- Creating a Cookie
- Reading a Cookie
- Setting and Reading a Cookie in the Same Page
- Using Sessions
- Creating a Session
- Setting Session Timeouts
- Using Applications
- Using Sessions, Applications, and JavaBeans
- Summary
- Q&A
- Workshop
Creating a Session
This is all best seen in an example. This next example will show how to store the number of times the user has accessed the page in the current session, as well as how to get the session ID, when the session was created, and the last time the page was accessed in the current session. This example starts with the page directive first discussed in Day 1, "Getting Started!," with the directive's session attribute set to true:
<%@page import = "java.util.*" session="true"%>
This makes sure that opening this page starts a new session if such a session doesn't already exist. Technically speaking, this page directive is not necessary, because the default for the session attribute is true, but it's included here just for completeness (you can omit it in your own session-based code).
This example will keep track of the number of times the user has viewed the current page in a session attribute named counter. The first thing to do is see if this attribute has already been set in a former page access in this session, which you can do with the getAttribute method.
Here's something to noteyou can't store the basic data types such as int in session attributesyou can only store Java objects, which are based on the java.lang.Object class (all Java objects are based on the same class). String objects are fine to store as attributesbut what about integer values such as our counter value? Java has a class to match all the basic data typesInteger for int values, Double for double values, and so on. You can create an object using these classes by passing a string (like 3) to the class's constructor, or by passing a value of the corresponding data type to the class's constructor (for example: Integer integerObject = new Integer(3) or Double doubleObject = new Double(3.14)). You can recover the data in the corresponding basic data form with the intValue method of the Integer class, doubleValue method of the Double class, and so on.
The counter variable will be stored as an Integer object, so here's how you can read its value from the session object using the getAttribute method:
<%@page import = "java.util.*" session="true"%> <HTML> <HEAD> <TITLE>Using Sessions</TITLE> </HEAD> <BODY> <% Integer counter = (Integer)session.getAttribute("counter"); . . .
If counter has not been set before, getAttribute will return a value of null. That means you can create the counter value, or increment it if it already exists, and store the new value in the session object like the following:
<%@page import = "java.util.*" session="true"%> <HTML> <HEAD> <TITLE>Using Sessions</TITLE> </HEAD> <BODY> <% Integer counter = (Integer)session.getAttribute("counter"); if (counter == null) { counter = new Integer(1); } else { counter = new Integer(counter.intValue() + 1); } session.setAttribute("counter", counter); . . .
That way you can store and retrieve data in the session object. You can also get the current session ID with the getID method. You get the time the session was created with the getCreationTime method, and you get the time the session was last accessed with the getLastAccessedTime method. You can see all this at work in Listing 7.5.
Listing 7.5 Creating and Using a Session (ch07_05.jsp)
<%@page import = "java.util.*" session="true"%> <HTML> <HEAD> <TITLE>Using Sessions to Track Users</TITLE> </HEAD> <BODY> <% Integer counter = (Integer)session.getAttribute("counter"); if (counter == null) { counter = new Integer(1); } else { counter = new Integer(counter.intValue() + 1); } session.setAttribute("counter", counter); %> <H1>Using Sessions to Track Users</H1> Session ID: <%=session.getId()%> <BR> Session creation time: <%=new Date(session.getCreationTime())%> <BR> Last accessed time: <%=new Date(session.getLastAccessedTime())%> <BR> Number of times you've been here: <%=counter%> </BODY> </HTML>
You can see the results in Figure 7.6, where the user has opened this page and reloaded it a number of times.
Figure 7.6 Creating and using a session.
Using sessions such as this is great for storing and recovering datait provides you with an environment much like a standard program, where you interact with the user without having to worry about having your data reset.