session的常用方法和request,ServletContext的区别

首先说一下session是如何使用的。其实很简单,第一步是要获取到session对象,然后第二部就是对数据添加、删除、获取操作了。
这里先说说session的寿命问题,session的寿命是可以在web.xml当中设置的,单位是分钟。比如如果寿命是120分钟,那么当同一个客户在上一次访问以后120分钟内没有再次访问的话session就会被注销,也就是说如果在120分钟内访问过了的话,那么就再延续120分钟。
获取session:
因为session是由服务器自动管理的,因此session的获取不可以直接new,而是要通过request或者servletcontext的方法获得,一般是:
HttpSession session=request.getSession();
这样就拿到session了。我们知道不同的客户端有自己的session,这个没有影响,只要通过上面的方法我们就可以获取到当前访问的request的session。
使用session:
session的使用类似于使用Map,是以key-value形式存储数据的。
主要方法是这几个:setAttribute(String name, Object value)、getAttribute(String name)、removeAttribute(String name)
这三个方法分别是添加(修改)值、获取值、删除值。
还有一个方法:invalidate() 用来注销该用户的session,注销以后下次该用户发出request的时候会获得一个新的session。
接下来列出HttpSession的所有方法:由于英语能力有限因此保留英文解释。
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.
获取以name为key的属性值,返回值是object
Enumeration getAttributeNames()
Returns an Enumeration of String objects containing the names of all the objects bound to this session.
获取一个包含有所有key的迭代器对象
long getCreationTime()
Returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT.
获取这个session的创建时间,返回值是一个long,GMT时间
String getId()
Returns a string containing the unique identifier assigned to this session.
获取这个session的jsessionid
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.
获取上一次该客户访问的时间。返回值是一个long,GMT时间
int getMaxInactiveInterval()
Returns the maximum time interval, in seconds, that the servlet container will keep this session open between client accesses.
获取session寿命的最大值。返回值是一个int,单位是秒。
ServletContext getServletContext()
Returns the ServletContext to which this session belongs.
获取这个项目的ServletContext对象。
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.
这个方法已经被淘汰了。
Object getValue(String name)
Deprecated. As of Version 2.2, this method is replaced by getAttribute(java.lang.String).
这个方法已经被getAttribute方法代替了。
String[] getValueNames()
Deprecated. As of Version 2.2, this method is replaced by getAttributeNames()
这个方法已经被getAttributeNames()方法代替了。
void invalidate()
Invalidates this session then unbinds any objects bound to it.
注销这个session。
boolean isNew()
Returns true if the client does not yet know about the session or if the client chooses not to join the session.
当客户端还不知道这个session的id或者客户端不适用session的时候返回true
一般发生在客户端第一次访问的时候。
void putValue(String name, Object value)
Deprecated. As of Version 2.2, this method is replaced by setAttribute(java.lang.String, java.lang.Object)
这个方法已经被setAttribute代替了
void removeAttribute(String name)
Removes the object bound with the specified name from this session.
删除以name为key的值。
void removeValue(String name)
Deprecated. As of Version 2.2, this method is replaced by removeAttribute(java.lang.String)
这个方法已经被removeAttribute方法代替了。
void setAttribute(String name, Object value)
Binds an object to this session, using the name specified.
在session当中添加一个以name为key以value为值的一组数据。
void setMaxInactiveInterval(int interval)
Specifies the time, in seconds, between client requests before the servlet container will invalidate this session.
设置当前session的寿命,单位是秒。
现在说说session、request、和ServletContext的区别。
session、request、和ServletContext(也叫做application)都是用来保存并且传递数据的。
三者不同的地方主要在于作用范围和生命周期。
session的生命周期时间一般是在web.xml当中设置。
request的生命周期是一次访问。也就是说客户端的一次对服务器的访问会发出一个request,那么这个request持续到服务器程序对这个request全部处理完成。
ServletContext的生命周期是这个应用的全程。也就是说从服务器应用启动开始ServletContext被加载起来,然后直到服务器关闭的时候销毁。这里的应用可以理解为开发时候的工程,也就是说一个工程有且只有一个ServletContext。
从作用范围来说,
session的作用范围是针对一个客户端,session对象和客户端是一一对应的。
request的作用范围是一个客户端的一次请求。
ServletContext的作用范围是服务器启动全程,并且针对所有的客户端。
综上所述,一个客户端在session的生命周期内访问服务器端的时候每次获取的session都是同一个。而一个客户端在一次请求访问服务器的时候,无论这次访问涉及到了多少servlet和jsp,其中的request对象都是同一个。无论多少个客户端访问服务器同一个应用,这个服务器应用在处理数据的时候ServletContext全是同一个对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值