Using the Session and Application Objects


Online programs are by default statelessthat is, when you load a page a number of times in succession, the data in the page is reinitialized each time. If you want to track, say, the number of times the user has accessed the page, you need to specifically store that data somewhere.

In a standard desktop program, where you're interacting with the user while the program is running, you can store data in variables. That data won't be reset to its default values the next time you look at it. Using JSP sessions, you actually can do the same thing in JSPyou can interact with the user as part of a "session."

While the session is active, you can store data in it, and that data will be preserved on the server in between user accesses to the page. You can set your variables in one page access, and they'll hold the same data the next time you see the page, as long as the session hasn't timed out (the default session timeout is 30 minutes).

JSP session objects are based on the javax.servlet.http.HttpSession interface. The data you store in a session is stored as session attributes. You can use the session object's setAttribute method to store data, and you can use the getAttribute method to recover that data as long as the session is active. You'll find the significant methods of the javax.servlet.http.HttpSession interface in Table 5.2.

Table 5.2. The Significant javax.servlet.http.HttpSession Methods

Method

Does this

void addCookie(Cookie cookie)

Adds the specified cookie to the response object

java.lang.Object getAttribute(java.lang. String name)

Returns the object of the given name in this session

java.util.Enumeration getAttributeNames()

Returns a Java Enumeration of String objects containing the names of all the objects in this session

long getCreationTime()

Returns the time when this session was created (measured in milliseconds since midnight January 1, 1970 GMT)

long getLastAccessedTime()

Returns the last time the client sent a request in with this session, as the number of milliseconds since midnight January 1, 1970 GMT

int getMaxInactiveInterval()

Returns the maximum time, in seconds, that the server will keep this session open between client accesses

void invalidate()

Invalidates this session

boolean isNew()

Returns a value of TRue if the client does not yet know about the session

void removeAttribute(java.lang.String name)

Removes the object with the specified name from this session

void setAttribute(java.lang.String name, java.lang.Object value)

Connects an object to this session, using the given name

void setMaxInactiveInterval(int interval)

Specifies the time, in seconds, between client requests before the server will invalidate this session


This is all best seen in an example. For instance, say you were trying to create a hit counter and therefore needed to store the number of times a page has been viewed. You're not going to get very far doing something like this, where you simply increment a variable each time a page is accessed, as shown in ch05_06.jsp:

 <HTML>     <HEAD>         <TITLE>A non-working hit counter</TITLE>     </HEAD>     <BODY>         <H1>A non-working hit counter</H1>         <%             int counter = 0;             counter++;         %>         Number of times you've been here: <%=counter%>     </BODY> </HTML> 

The problem is that each time the JSP is displayed, all the data in it is reinitialized, which means that the counter variable will never hold more than 1, no matter how many times you view the page, as shown in Figure 5.8.

Figure 5.8. A non-working hit counter.




    Java After Hours(c) 10 Projects You'll Never Do at Work
    Java After Hours: 10 Projects Youll Never Do at Work
    ISBN: 0672327473
    EAN: 2147483647
    Year: 2006
    Pages: 128

    Similar book on Amazon

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