Java The Complete Reference, Seventh Edition

(Greg DeLong) #1
The HttpSessionBindingListener Interface

TheHttpSessionBindingListenerinterface is implemented by objects that need to be
notified when they are bound to or unbound from an HTTP session. The methods that are
invoked when an object is bound or unbound are


void valueBound(HttpSessionBindingEvente)
void valueUnbound(HttpSessionBindingEvente)

Here,eis the event object that describes the binding.


The Cookie Class

TheCookieclass encapsulates a cookie. Acookieis stored on a client and contains state
information. Cookies are valuable for tracking user activities. For example, assume that a


Chapter 31: Servlets 919


Method Description
Object getAttribute(Stringattr) Returns the value associated with the name passed inattr.Returns
nullifattris not found.
Enumeration getAttributeNames( ) Returns an enumeration of the attribute names associated with the
session.
long getCreationTime( ) Returns the time (in milliseconds since midnight, Januar y 1, 1970,
GMT) when this session was created.
String getId( ) Returns the session ID.
long getLastAccessedTime( ) Returns the time (in milliseconds since midnight, Januar y 1, 1970,
GMT) when the client last made a request for this session.
void invalidate( ) Invalidates this session and removes it from the context.
boolean isNew( ) Returnstrueif the ser ver created the session and it has not yet
been accessed by the client.
void removeAttribute(Stringattr) Removes the attribute specified byattrfrom the session.
void setAttribute(Stringattr, Objectval) Associates the value passed invalwith the attribute name passed
inattr.

TABLE 31-7 The Methods Defined byHttpSession


Method Description
void sendError(intc)
throws IOException

Sends the error codecto the client.

void sendError(intc, Strings)
throws IOException

Sends the error codecand messagesto the client.

void sendRedirect(Stringurl)
throws IOException

Redirects the client tourl.

void setDateHeader(Stringfield, longmsec) Addsfieldto the header with date value equal tomsec
(milliseconds since midnight, Januar y 1, 1970, GMT).
void setHeader(Stringfield, Stringvalue) Addsfieldto the header with value equal tovalue.
void setIntHeader(Stringfield, intvalue) Addsfieldto the header with value equal tovalue.
void setStatus(intcode) Sets the status code for this response tocode.

TABLE 31-6 Various Methods Defined byHttpServletResponse(continued)

Free download pdf