/** * * Returns the time when this session was created, measured * in milliseconds since midnight January 1, 1970 GMT. * * @return a <code>long</code> specifying * when this session was created, * expressed in * milliseconds since 1/1/1970 GMT * * @exception IllegalStateException if this method is called on an * invalidated session * */ public long getCreationTime(); /** * * Returns a string containing the unique identifier assigned * to this session. The identifier is assigned * by the servlet container and is implementation dependent. * * @return a string specifying the identifier * assigned to this session * * @exeption IllegalStateException if this method is called on an * invalidated session * */ public String getId(); /** * * 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 ed by the time the container recieved the request. * * <p>Actions that your application takes, such as getting or setting * a value associated with the session, do not affect the access * time. * * @return a <code>long</code> * representing the last time * the client sent a request associated * with this session, expressed in * milliseconds since 1/1/1970 GMT * * @exeption IllegalStateException if this method is called on an * invalidated session * */ public long getLastAccessedTime(); /** * Returns the ServletContext to which this session belongs. * * @return The ServletContext object for the web application * @since 2.3 */ public ServletContext getServletContext(); /** * * 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. * * @param interval An integer specifying the number * of seconds * */
public void setMaxInactiveInterval(int interval);
/** * * @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 HttpSessionContext getSessionContext(); /** * * Returns the object bound with the specified name in this session, or * <code>null</code> if no object is bound under the name. * * @param name a string specifying the name of the object * * @return the object with the specified name * * @exception IllegalStateException if this method is called on an * invalidated session * */
public Object getAttribute(String name);
/** * * @deprecated As of Version 2.2, this method is * replaced by {@link #getAttribute}. * * @param name a string specifying the name of the object * * @return the object with the specified name * * @exception IllegalStateException if this method is called on an * invalidated session * */
public Object getValue(String name); /** * * Returns an <code>Enumeration</code> of <code>String</code> objects * containing the names of all the objects bound to this session. * * @return an <code>Enumeration</code> of * <code>String</code> objects specifying the * names of all the objects bound to * this session * * @exception IllegalStateException if this method is called on an * invalidated session * */
public Enumeration getAttributeNames();
/** * * @deprecated As of Version 2.2, this method is * replaced by {@link #getAttributeNames} * * @return an array of <code>String</code> * objects specifying the * names of all the objects bound to * this session * * @exception IllegalStateException if this method is called on an * invalidated session * */
public String[] getValueNames();
/** * 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. * * <p>After this method executes, and if the new object * implements <code>HttpSessionBindingListener</code>, * the container calls * <code>HttpSessionBindingListener.valueBound</code>. The container then * notifies any <code>HttpSessionAttributeListener</code>s in the web * application.
* <p>If an object was already bound to this session of this name * that implements <code>HttpSessionBindingListener</code>, its * <code>HttpSessionBindingListener.valueUnbound</code> method is called. * * <p>If the value passed in is null, this has the same effect as calling * <code>removeAttribute()<code>. * * * @param name the name to which the object is bound; * cannot be null * * @param value the object to be bound * * @exception IllegalStateException if this method is called on an * invalidated session * */
public void setAttribute(String name, Object value);
/** * * @deprecated As of Version 2.2, this method is * replaced by {@link #setAttribute} * * @param name the name to which the object is bound; * cannot be null * * @param value the object to be bound; cannot be null * * @exception IllegalStateException if this method is called on an * invalidated session * */
public void putValue(String name, Object value); /** * * 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. * * <p>After this method executes, and if the object * implements <code>HttpSessionBindingListener</code>, * the container calls * <code>HttpSessionBindingListener.valueUnbound</code>. The container * then notifies any <code>HttpSessionAttributeListener</code>s in the web * application. * * * * @param name the name of the object to * remove from this session * * @exception IllegalStateException if this method is called on an * invalidated session */ public void removeAttribute(String name); /** * * @deprecated As of Version 2.2, this method is * replaced by {@link #removeAttribute} * * @param name the name of the object to * remove from this session * * @exception IllegalStateException if this method is called on an * invalidated session */ public void removeValue(String name); /** * * Invalidates this session then unbinds any objects bound * to it. * * @exception IllegalStateException if this method is called on an * already invalidated session * */ public void invalidate();
/** * * Returns <code>true</code> 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. * * @return <code>true</code> if the * server has created a session, * but the client has not yet joined * * @exception IllegalStateException if this method is called on an * already invalidated session * */ public boolean isNew();
*
* Returns the time when this session was created, measured
* in milliseconds since midnight January 1, 1970 GMT.
*
* @return a <code>long</code> specifying
* when this session was created,
* expressed in
* milliseconds since 1/1/1970 GMT
*
* @exception IllegalStateException if this method is called on an
* invalidated session
*
*/ public long getCreationTime();
/**
*
* Returns a string containing the unique identifier assigned
* to this session. The identifier is assigned
* by the servlet container and is implementation dependent.
*
* @return a string specifying the identifier
* assigned to this session
*
* @exeption IllegalStateException if this method is called on an
* invalidated session
*
*/ public String getId();
/**
*
* 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 ed by the time the container recieved the request.
*
* <p>Actions that your application takes, such as getting or setting
* a value associated with the session, do not affect the access
* time.
*
* @return a <code>long</code>
* representing the last time
* the client sent a request associated
* with this session, expressed in
* milliseconds since 1/1/1970 GMT
*
* @exeption IllegalStateException if this method is called on an
* invalidated session
*
*/ public long getLastAccessedTime(); /**
* Returns the ServletContext to which this session belongs.
*
* @return The ServletContext object for the web application
* @since 2.3
*/ public ServletContext getServletContext(); /**
*
* 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.
*
* @param interval An integer specifying the number
* of seconds
*
*/
public void setMaxInactiveInterval(int interval);
*
* @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 HttpSessionContext getSessionContext(); /**
*
* Returns the object bound with the specified name in this session, or
* <code>null</code> if no object is bound under the name.
*
* @param name a string specifying the name of the object
*
* @return the object with the specified name
*
* @exception IllegalStateException if this method is called on an
* invalidated session
*
*/
public Object getAttribute(String name);
/**
*
* @deprecated As of Version 2.2, this method is
* replaced by {@link #getAttribute}.
*
* @param name a string specifying the name of the object
*
* @return the object with the specified name
*
* @exception IllegalStateException if this method is called on an
* invalidated session
*
*/
public Object getValue(String name); /**
*
* Returns an <code>Enumeration</code> of <code>String</code> objects
* containing the names of all the objects bound to this session.
*
* @return an <code>Enumeration</code> of
* <code>String</code> objects specifying the
* names of all the objects bound to
* this session
*
* @exception IllegalStateException if this method is called on an
* invalidated session
*
*/
public Enumeration getAttributeNames();
/**
*
* @deprecated As of Version 2.2, this method is
* replaced by {@link #getAttributeNames}
*
* @return an array of <code>String</code>
* objects specifying the
* names of all the objects bound to
* this session
*
* @exception IllegalStateException if this method is called on an
* invalidated session
*
*/
public String[] getValueNames();
/**
* 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.
*
* <p>After this method executes, and if the new object
* implements <code>HttpSessionBindingListener</code>,
* the container calls
* <code>HttpSessionBindingListener.valueBound</code>. The container then
* notifies any <code>HttpSessionAttributeListener</code>s in the web
* application.
* <p>If an object was already bound to this session of this name
* that implements <code>HttpSessionBindingListener</code>, its
* <code>HttpSessionBindingListener.valueUnbound</code> method is called.
*
* <p>If the value passed in is null, this has the same effect as calling
* <code>removeAttribute()<code>.
*
*
* @param name the name to which the object is bound;
* cannot be null
*
* @param value the object to be bound
*
* @exception IllegalStateException if this method is called on an
* invalidated session
*
*/
public void setAttribute(String name, Object value);
*
* @deprecated As of Version 2.2, this method is
* replaced by {@link #setAttribute}
*
* @param name the name to which the object is bound;
* cannot be null
*
* @param value the object to be bound; cannot be null
*
* @exception IllegalStateException if this method is called on an
* invalidated session
*
*/
public void putValue(String name, Object value);
/**
*
* 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.
*
* <p>After this method executes, and if the object
* implements <code>HttpSessionBindingListener</code>,
* the container calls
* <code>HttpSessionBindingListener.valueUnbound</code>. The container
* then notifies any <code>HttpSessionAttributeListener</code>s in the web
* application.
*
*
*
* @param name the name of the object to
* remove from this session
*
* @exception IllegalStateException if this method is called on an
* invalidated session
*/ public void removeAttribute(String name);
/**
*
* @deprecated As of Version 2.2, this method is
* replaced by {@link #removeAttribute}
*
* @param name the name of the object to
* remove from this session
*
* @exception IllegalStateException if this method is called on an
* invalidated session
*/ public void removeValue(String name); /**
*
* Invalidates this session then unbinds any objects bound
* to it.
*
* @exception IllegalStateException if this method is called on an
* already invalidated session
*
*/ public void invalidate();
/**
*
* Returns <code>true</code> 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.
*
* @return <code>true</code> if the
* server has created a session,
* but the client has not yet joined
*
* @exception IllegalStateException if this method is called on an
* already invalidated session
*
*/ public boolean isNew();
每个会话应该只对应于一个客户,并且可以跨多个请求。会话通常通过URL重写或cookie来跟踪,但是跟踪进行请求的客户的方法对于session对象并不重要。
session对象是javax.servlet.http.HttpSession的实例,其行为方式与Java Servlet下的session对显完全相同。
希望对你有所帮助!!
String tag=(String)session.getValue("tag");
if(tag.equals("我是乔锋我怕谁")) out.println("我现在时段誉了");
板桥里人 jdon.comJsp的session是使用bean的一个生存期限,一般为page,session意思是在这个用户没有离开网站之前一直有效,如果无法判断用户何时离开,一般依据系统设定,tomcat中设定为30分钟.我们使用seesion功能,可以达到多个jsp程序从操作同一个java bean, 那么这个java bean可以作为我们传统意义上的"全局变量池".(在java中我们可以使用static静态化一个变量和方法,使用singleton唯一化对象.)在项目实践中,我们Jsp程序中很多参数需要从数据库中读取,有的参数实际读取一次就可以,如果设计成每个用户每产生一个页面都要读取数据库,很显然,数据库的负载很大,同时也浪费时间,虽然可能有数据库连接池优化,但是尽量少使用数据库是我们编程的原则.比如,我们的test.jsp 和test1.jsp都需要得到一个参数userdir,这个userdir是从数据库中得知,使用session将大大优化性能,程序如下:设计一个javabean 存储userdir.public class UserEnv {private String userdir = "";
private String userurl = "";public UserEnv(){
//构建方法初始化userdir,可以从数据库中读取,这里简单给值ppp
userdir="pppp";
System.out.println("init userdir, one time");
} public String getUserdir() throws Exception{
return userdir;} }test1.jsp程序:
<%@ page contentType="text/html;charset=ISO8859_1" %> <jsp:useBean id="myenv" scope="session" class="mysite.UserEnv"/>
<html>
<head>
<title>Untitled</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><body>this is test1.jsp:<%=myenv.getUserdir()%>
</body>
</html>
test2.jsp程序:
<%@ page contentType="text/html;charset=ISO8859_1" %> <jsp:useBean id="myenv" scope="session" class="mysite.UserEnv"/>
<html>
<head>
<title>Untitled</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><body>this is test2.jsp:<%=myenv.getUserdir()%>
</body>
</html> 无论用户先调用test1.jsp还是test2.jsp, java bean UserEnv总是先初始化一次, 由于这个bean存在周期是seesion,因此该用户第二次以后只要在seesion有效期内再调用,myenv.getUserdir()将直接从bean内存中读取变量,不必再初始化.这样提高速度,又减少数据库访问量.这样,我们就有了一个jsp程序之间共享变量或方法 的实现办法.
MHDN.net
会话状态维持是 Web 应用开发者必须面对的问题。有多种方法可以用来解决这个问题,如使用 Cookies 、隐藏的表单输入域,或直接将状态信息附加到 URL 中。 Java Servlet 提供了一个在多个请求之间持续有效的会话对象,该对象允许用户存储和提取会话状态信息。 JSP 也同样支持 Servlet 中的这个概念。
在 Sun 的 JSP 指南 中可以看到许多有关隐含对象的说明(隐含的含义是,这些对象可以直接引用,不需要显式地声明,也不需要专门的代码创建其实例)。例如 request 对象,它是 HttpServletRequest 的一个子类。该对象包含了所有有关当前浏览器请求的信息,包括 Cookies , HTML 表单变量等等。 session 对象也是这样一个隐含对象。这个对象在第一个 JSP 页面被装载时自动创建,并被关联到 request 对象上。与 ASP 中的会话对象相似, JSP 中的 session 对象对于那些希望通过多个页面完成一个事务的应用是非常有用的。 为说明 session 对象的具体应用,接下来我们用三个页面模拟一个多页面的 Web 应用。第一个页面( q1.html )仅包含一个要求输入用户名字的 HTML 表单,代码如下: < HTML> < BODY> < FORM METHOD=POST ACTION=".jsp"> 请输入您的姓名: < INPUT TYPE=TEXT NAME="thename"> < INPUT TYPE=SUBMIT VALUE="SUBMIT"> < /FORM> < /BODY> < /HTML> 第二个页面是一个 JSP 页面( .jsp ),它通过 request 对象提取 q1.html 表单中的 thename 值,将它存储为 name 变量,然后将这个 name 值保存到 session 对象中。 session 对象是一个名字 / 值对的集合,在这里,名字 / 值对中的名字为“ thename ”,值即为 name 变量的值。由于 session 对象在会话期间是一直有效的,因此这里保存的变量对后继的页面也有效。 .jsp 的另外一个任务是询问第二个问题。下面是它的代码: < HTML> < BODY> < %@ page language="java" %> < %! String name=""; %> < % name = request.getParameter("thename"); session.putValue("thename", name); %> 您的姓名是: < %= name %> < p> < FORM METHOD=POST ACTION="q3.jsp"> 您喜欢吃什么 ? < INPUT TYPE=TEXT NAME="food"> < P> < INPUT TYPE=SUBMIT VALUE="SUBMIT"> < /FORM> < /BODY> < /HTML> 第三个页面也是一个 JSP 页面( q3.jsp ),主要任务是显示问答结果。它从 session 对象提取 thename 的值并显示它,以此证明虽然该值在第一个页面输入,但通过 session 对象得以保留。 q3.jsp 的另外一个任务是提取在第二个页面中的用户输入并显示它: < HTML> < BODY> < %@ page language="java" %> < %! String food=""; %> < % food = request.getParameter("food"); String name = (String) session.getValue("thename"); %> 您的姓名是: < %= name %> < P> 您喜欢吃: < %= food %> < /BODY> < /HTML>