19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

«interface»<br />

javax.servlet.http.HttpSession<br />

+getAttribute(name: String): Object<br />

+setAttribute(name: String, value: Object):<br />

void<br />

+getId(): String<br />

+getLastAccessedTime(): long<br />

+invalidate(): void<br />

+isNew(): boolean<br />

+removeAttribute(name: String): void<br />

+getMaxInactiveInterval(): int<br />

+setMaxInactiveInterval(interval: int): void<br />

Figure 42.24<br />

Returns the object bound with the specified name in this session, or null if no<br />

object is bound under the name.<br />

Binds an object <strong>to</strong> this session, using the specified name. If an object of the<br />

same name is already bound <strong>to</strong> the session, the object is replaced.<br />

Returns a string containing the unique identifier assigned <strong>to</strong> this session. The<br />

identifier is assigned by the servlet container and is implementation<br />

dependent.<br />

Returns the last time the client sent a request associated with this session, as the<br />

number of milliseconds since midnight January 1, 1970 GMT, and marked by<br />

the time the container received the request.<br />

Invalidates this session, then unbinds any objects bound <strong>to</strong> it.<br />

Returns true if the session was just created in the current request.<br />

Removes the object bound with the specified name from this session. If the<br />

session does not have an object bound with the specified name, this method does<br />

nothing.<br />

Returns the time, in seconds, between client requests before the servlet container<br />

will invalidate this session. A negative time indicates that the session will<br />

never time-out. Use setMaxInactiveInterval <strong>to</strong> specify this value.<br />

HttpSession establishes a persistent session between a client with<br />

multiple requests and the server.<br />

NOTE: HTTP is stateless. So how does the server associate<br />

a session with multiple requests from the same client?<br />

This is handled behind the scenes by the servlet<br />

container and is transparent <strong>to</strong> the servlet programmer.<br />

To demonstrate using HttpSession, let us rewrite Listing 42.9,<br />

Registration.java, and Listing 42.10, RegistrationWithCookie.java.<br />

Instead of using hidden values or cookies for session tracking, it uses<br />

servlet HttpSession.<br />

Create the servlet named RegistrationWithHttpSession in Listing 42.11.<br />

Create an HTML file named RegistrationWithHttpSession.html that is<br />

identical <strong>to</strong> Registration.html except that the action is replaced by<br />

RegistrationWithHttpSessionH.<br />

Listing 42.11 RegistrationWithHttpSession.java<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

package chapter42;<br />

import javax.servlet.*;<br />

import javax.servlet.http.*;<br />

import java.io.*;<br />

import java.sql.*;<br />

38

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!