HttpSession (Java EE 5)

Java EE API


javax.servlet.http Interface HttpSession


public interface HttpSession


提供一种方式,跨多个页面请求或对 Web 站点的多次访问标识用户并存储有关该用户的信息。

servlet 容器使用此接口创建 HTTP 客户端和 HTTP 服务器之间的会话。会话将保留指定的时间段,跨多个连接或来自用户的页面请求。一个会话通常对应于一个用户,该用户可能多次访问一个站点。服务器能够以多种方式维护会话,比如使用 cookie 或重写 URL。

此接口允许 servlet

  • 查看和操作有关某个会话的信息,比如会话标识符、创建时间和最后一次访问时间
  • 将对象绑定到会话,允许跨多个用户连接保留用户信息

当应用程序将对象存储到会话中或从会话中移除对象时,该会话将检查对象是否实现了 HttpSessionBindingListener。如果实现了,则 servlet 将通知该对象它已经被绑定到会话,或者已从会话中取消对它的绑定。通知是在绑定方法完成后发送的。对于无效或过期的会话,通知是在会话已经无效或过期之后发送的。

当容器使用分布式容器设置在 VM 之间迁移会话时,所有实现 HttpSessionActivationListener 接口的会话属性都会得到通知。

servlet 应该能够处理客户端选择不加入会话的情况,比如故意关闭 cookie 时。在客户端加入会话前,isNew 一直返回 true。如果客户端选择不加入会话,则 getSession 将对每个请求返回一个不同的会话,并且 isNew 将总是返回 true

会话信息的范围仅限于当前 Web 应用程序 (ServletContext),因此存储在一个上下文中的信息在另一个上下文中不是直接可见的。

英文文档:

Provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user.

The servlet container uses this interface to create a session between an HTTP client and an HTTP server. The session persists for a specified time period, across more than one connection or page request from the user. A session usually corresponds to one user, who may visit a site many times. The server can maintain a session in many ways such as using cookies or rewriting URLs.

This interface allows servlets to

  • View and manipulate information about a session, such as the session identifier, creation time, and last accessed time
  • Bind objects to sessions, allowing user information to persist across multiple user connections

When an application stores an object in or removes an object from a session, the session checks whether the object implements HttpSessionBindingListener. If it does, the servlet notifies the object that it has been bound to or unbound from the session. Notifications are sent after the binding methods complete. For session that are invalidated or expire, notifications are sent after the session has been invalidated or expired.

When container migrates a session between VMs in a distributed container setting, all session attributes implementing the HttpSessionActivationListener interface are notified.

A servlet should be able to handle cases in which the client does not choose to join a session, such as when cookies are intentionally turned off. Until the client joins the session, isNew returns true. If the client chooses not to join the session, getSession will return a different session on each request, and isNew will always return true.

Session information is scoped only to the current web application (ServletContext), so information stored in one context will not be directly visible in another.

Author:
Various
See Also:
HttpSessionBindingListener, HttpSessionContext

Method Summary
 Object
 Enumeration
 long
 String
 long
 int
 ServletContext
 HttpSessionContext
 Object
 String[]
 void
 boolean
 void
 void
 void
 void
 void
 

Method Detail

public long getCreationTime()
返回创建此会话的时间,该时间是用自格林威治标准时间 1970 年 1 月 1 日午夜起经过的毫秒数来测量的。
return 指定创建此会话的时间的 long 值,用自格林威治标准时间 1970 年 1 月 1 日起经过的毫秒数表示
ThrowsIllegalStateException: 如果对无效的会话调用此方法
英文文档:

getCreationTime

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

Returns:
a long specifying when this session was created, expressed in milliseconds since 1/1/1970 GMT
Throws:
IllegalStateException - if this method is called on an invalidated session

public String getId()
返回包含分配给此会话的唯一标识符的字符串。标识符是由 servlet 容器分配的,并且是与实现相关的。
return 指定分配给此会话的标识符的字符串
英文文档:

getId

String getId()
Returns a string containing the unique identifier assigned to this session. The identifier is assigned by the servlet container and is implementation dependent.

Returns:
a string specifying the identifier assigned to this session

public long getLastAccessedTime()
返回客户端上一次发送与此会话关联的请求的时间(该时间是自格林威治标准时间 1970 年 1 月 1 日午夜起经过的毫秒数),并用容器接收该请求的时间标记它。

应用程序采取的操作(比如获取或设置与会话关联的值)不会影响访问时间。

return 表示客户端上一次发送与此会话关联的请求的时间的 long 值,该时间用自格林威治标准时间 1970 年 1 月 1 日起经过的毫秒数表示
ThrowsIllegalStateException: 如果对无效的会话调用此方法

英文文档:

getLastAccessedTime

long getLastAccessedTime()
Returns the last time the client sent a request associated with this session, as the number of milliseconds since midnight January 1, 1970 GMT, and marked by the time the container received the request.

Actions that your application takes, such as getting or setting a value associated with the session, do not affect the access time.

Returns:
a long representing the last time the client sent a request associated with this session, expressed in milliseconds since 1/1/1970 GMT
Throws:
IllegalStateException - if this method is called on an invalidated session

public ServletContext getServletContext()
返回此会话所属的 ServletContext。
return Web 应用程序的 ServletContext 对象
since2.3
英文文档:

getServletContext

ServletContext getServletContext()
Returns the ServletContext to which this session belongs.

Returns:
The ServletContext object for the web application
Since:
2.3

public void setMaxInactiveInterval(int interval)
指定在 servlet 容器使此会话失效之前客户端请求之间的时间间隔,以秒为单位。负数时间指示会话永远不会超时。
interval 指定秒数的整数
英文文档:

setMaxInactiveInterval

void setMaxInactiveInterval(int interval)
Specifies the time, in seconds, between client requests before the servlet container will invalidate this session. A negative time indicates the session should never timeout.

Parameters:
interval - An integer specifying the number of seconds

public int getMaxInactiveInterval()
返回 servlet 容器在客户端访问之间将使此会话保持打开状态的最大时间间隔,以秒为单位。在此间隔之后,servlet 容器将使会话无效。可使用 setMaxInactiveInterval 方法设置最大时间间隔。负数时间指示会话永远不会超时。
return 指定此会话在客户端请求之间保持打开状态的秒数的整数
See also setMaxInactiveInterval
英文文档:

getMaxInactiveInterval

int getMaxInactiveInterval()
Returns the maximum time interval, in seconds, that the servlet container will keep this session open between client accesses. After this interval, the servlet container will invalidate the session. The maximum time interval can be set with the setMaxInactiveInterval method. A negative time indicates the session should never timeout.

Returns:
an integer specifying the number of seconds this session remains open between client requests
See Also:
setMaxInactiveInterval(int)

public HttpSessionContext getSessionContext()
deprecated 从版本 2.1 起,此方法已过时,并且无替代版本。此方法将在 Java Servlet API 将来的版本中移除。
英文文档:

getSessionContext

HttpSessionContext getSessionContext()
Deprecated. As of Version 2.1, this method is deprecated and has no replacement. It will be removed in a future version of the Java Servlet API.


public Object getAttribute(String name)
返回与此会话中的指定名称绑定在一起的对象,如果没有对象绑定在该名称下,则返回 null
name 指定对象名称的字符串
return 具有指定名称的对象
ThrowsIllegalStateException: 如果对无效的会话调用此方法
英文文档:

getAttribute

Object getAttribute(String name)
Returns the object bound with the specified name in this session, or null if no object is bound under the name.

Parameters:
name - a string specifying the name of the object
Returns:
the object with the specified name
Throws:
IllegalStateException - if this method is called on an invalidated session

public Object getValue(String name)
deprecated 从版本 2.2 起,此方法已由 #getAttribute 替代。
name 指定对象名称的字符串
return 具有指定名称的对象
ThrowsIllegalStateException: 如果对无效的会话调用此方法
英文文档:

getValue

Object getValue(String name)
Deprecated. As of Version 2.2, this method is replaced by getAttribute(java.lang.String).

Parameters:
name - a string specifying the name of the object
Returns:
the object with the specified name
Throws:
IllegalStateException - if this method is called on an invalidated session

public java.util.Enumeration<E> getAttributeNames()
返回包含绑定到此会话的所有对象的名称的 String 对象的 Enumeration
return 指定绑定到此会话的所有对象的名称的 String 对象的 Enumeration
ThrowsIllegalStateException: 如果对无效的会话调用此方法
英文文档:

getAttributeNames

Enumeration getAttributeNames()
Returns an Enumeration of String objects containing the names of all the objects bound to this session.

Returns:
an Enumeration of String objects specifying the names of all the objects bound to this session
Throws:
IllegalStateException - if this method is called on an invalidated session

public String[] getValueNames()
deprecated 从版本 2.2 起,此方法已由 #getAttributeNames 替代
return 指定绑定到此会话的所有对象的名称的 String 对象数组
ThrowsIllegalStateException: 如果对无效的会话调用此方法
英文文档:

getValueNames

String[] getValueNames()
Deprecated. As of Version 2.2, this method is replaced by getAttributeNames()

Returns:
an array of String objects specifying the names of all the objects bound to this session
Throws:
IllegalStateException - if this method is called on an invalidated session

public void setAttribute(String name, Object value)
使用指定名称将对象绑定到此会话。如果具有同样名称的对象已经绑定到该会话,则替换该对象。

在执行此方法后,如果新对象实现了 HttpSessionBindingListener,则容器将调用 HttpSessionBindingListener.valueBound。然后,容器通知 Web 应用程序中的任何 HttpSessionAttributeListener

如果某个对象已经使用此名称绑定到了此会话且该对象实现了 HttpSessionBindingListener,则调用它的 HttpSessionBindingListener.valueUnbound 方法。

如果传入的值为 null,则调用此方法将与调用 removeAttribute() 产生的效果相同。

name 对象绑定到的名称;不能为 null
value 要绑定的对象
ThrowsIllegalStateException: 如果对无效的会话调用此方法

英文文档:

setAttribute

void setAttribute(String name,
                  Object value)
Binds an object to this session, using the name specified. If an object of the same name is already bound to the session, the object is replaced.

After this method executes, and if the new object implements HttpSessionBindingListener, the container calls HttpSessionBindingListener.valueBound. The container then notifies any HttpSessionAttributeListeners in the web application.

If an object was already bound to this session of this name that implements HttpSessionBindingListener, its HttpSessionBindingListener.valueUnbound method is called.

If the value passed in is null, this has the same effect as calling removeAttribute().

Parameters:
name - the name to which the object is bound; cannot be null
value - the object to be bound
Throws:
IllegalStateException - if this method is called on an invalidated session

public void putValue(String name, Object value)
deprecated 从版本 2.2 起,此方法已由 #setAttribute 替代
name 对象绑定到的名称;不能为 null
value 要绑定的对象;不能为 null
ThrowsIllegalStateException: 如果对无效的会话调用此方法
英文文档:

putValue

void putValue(String name,
              Object value)
Deprecated. As of Version 2.2, this method is replaced by setAttribute(java.lang.String, java.lang.Object)

Parameters:
name - the name to which the object is bound; cannot be null
value - the object to be bound; cannot be null
Throws:
IllegalStateException - if this method is called on an invalidated session

public void removeAttribute(String name)
从此会话中移除与指定名称绑定在一起的对象。如果会话没有与指定名称绑定在一起的对象,则此方法不执行任何操作。

在执行此方法后,如果对象实现了 HttpSessionBindingListener,则容器将调用 HttpSessionBindingListener.valueUnbound。然后,容器通知 Web 应用程序中的任何 HttpSessionAttributeListener

name 要从此会话中移除的对象的名称
ThrowsIllegalStateException: 如果对无效的会话调用此方法

英文文档:

removeAttribute

void removeAttribute(String name)
Removes the object bound with the specified name from this session. If the session does not have an object bound with the specified name, this method does nothing.

After this method executes, and if the object implements HttpSessionBindingListener, the container calls HttpSessionBindingListener.valueUnbound. The container then notifies any HttpSessionAttributeListeners in the web application.

Parameters:
name - the name of the object to remove from this session
Throws:
IllegalStateException - if this method is called on an invalidated session

public void removeValue(String name)
deprecated 从版本 2.2 起,此方法已由 #removeAttribute 替代
name 要从此会话中移除的对象的名称
ThrowsIllegalStateException: 如果对无效的会话调用此方法
英文文档:

removeValue

void removeValue(String name)
Deprecated. As of Version 2.2, this method is replaced by removeAttribute(java.lang.String)

Parameters:
name - the name of the object to remove from this session
Throws:
IllegalStateException - if this method is called on an invalidated session

public void invalidate()
使此会话无效,然后取消对任何绑定到它的对象的绑定。
ThrowsIllegalStateException: 如果对已经无效的会话调用此方法
英文文档:

invalidate

void invalidate()
Invalidates this session then unbinds any objects bound to it.

Throws:
IllegalStateException - if this method is called on an already invalidated session

public boolean isNew()
如果客户端还不知道该会话,或者客户端选择不加入该会话,则返回 true。例如,如果服务器仅使用基于 cookie 的会话,而客户端已经禁止了 cookie 的使用,则每个请求上的会话都将是新会话。
return 如果服务器已经创建了一个会话,但客户端尚未加入该会话,则返回 true
ThrowsIllegalStateException: 如果对已经无效的会话调用此方法
英文文档:

isNew

boolean isNew()
Returns true if the client does not yet know about the session or if the client chooses not to join the session. For example, if the server used only cookie-based sessions, and the client had disabled the use of cookies, then a session would be new on each request.

Returns:
true if the server has created a session, but the client has not yet joined
Throws:
IllegalStateException - if this method is called on an already invalidated session


Submit a bug or feature

Copyright 2007 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.

一看就知道只有菜鸟才干这么无知的事啦。

PS : 未经我党受权你也可自由散发此文档。 如有任何错误请自行修正;若因此而造成任何损失请直接找人民主席,请勿与本人联系。谢谢!