Java After Hours: 10 Projects Youll Never Do at Work

You need some way of storing data between page accesses, and one way of doing that is to use the session object, which comes built in to the JSP environment, ready for you to use. To use this object, you start with the JSP page directive at the top of the JSP page, setting this directive's session attribute to TRue to indicate that you want to use sessions:

<%@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.

NOTE

The page directive is not really necessary here, because the default for the session attribute is true, but it's included here just for completeness; you can omit it in your own code. More on this directive in Chapter 10, "Getting a Graphical Weather Forecast: The Forecaster Project."

You can keep track of the number of times the user has viewed the page in a session attribute named, say, counter. You store data in sessions using attributes, and you can store and retrieve data in sessions using the setAttribute and getAttribute methods.

You can't store the basic data types such as int in session attributesyou can only store Java objects, based on the java.lang.Object class. String objects are fine to store as attributes, but what about int values such as the counter value? In this case, you should use the formal Integer class, not just try to store an int value. You can create a Integer object with that class's constructor, and you can recover the data in the object with the intValue method of the Integer class.

Okay, this is ready to go. First, the code will check if the counter attribute has already been set in a former page access. The counter variable will be stored as an Integer object, so here's how you can read its current value from the session object using the getAttribute method:

<%@page import = "java.util.*" session="true"%> <HTML> <HEAD> <TITLE>A hit counter using sessions</TITLE> </HEAD> <BODY> <H1>A hit counter using sessions</H1> <% Integer counter = (Integer)session.getAttribute("counter"); . . .

If counter has not been set before, getAttribute will return a value of null. In that case, you should create the counter value. On the other hand, if it already exists, you should increment the value stored in it and store the new value in the session object, like this:

<%@page import = "java.util.*" session="true"%> <HTML> <HEAD> <TITLE>A hit counter using sessions</TITLE> </HEAD> <BODY> <% <H1>A hit counter using sessions</H1> 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's how you can store and retrieve data in the session object. Here's what it looks like in a new, working hit counter, ch05_07.jsp:

<%@page import = "java.util.*" session="true"%> <HTML> <HEAD> <TITLE>A hit counter using sessions</TITLE> </HEAD> <BODY> <H1>A hit counter using sessions</H1> <% Integer counter = (Integer)session.getAttribute("counter"); if (counter == null) { counter = new Integer(1); } else { counter = new Integer(counter.intValue() + 1); } session.setAttribute("counter", counter); %> Number of times you've been here: <%=counter%> </BODY> </HTML>

The results are shown in Figure 5.9, where the user has opened this page and reloaded it a number of times, as you can see, because the page says the user has been here three times.

Figure 5.9. Using a session-based hit counter.

Using sessions like this is great for storing and recovering data, as you can seeit 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.

That's fine, except for one thinghow do you share data between users? What if 12 users are accessing chat.jsp at once? Every time a new user loads chat.jsp, a new session object would be created, which means you can't share data between users this way.

But there is a new object, similar to the session object, that you can use and will do the trickthe application object.

    Категории