`
ssh2
  • 浏览: 48876 次
  • 性别: Icon_minigender_1
  • 来自: 绍兴
社区版块
存档分类
最新评论

理解javax.servlet.http.HttpSession

阅读更多
理解javax.servlet.http.HttpSession
  HttpSession是Java平台对session机制的实现规范,因为它仅仅是个接口,具体到每个web应用服务器的提供商,除了对规范支持之外,仍然会有一些规范里没有规定的细微差异。这里我们以BEA的Weblogic Server8.1作为例子来演示。
  首先,Weblogic Server提供了一系列的参数来控制它的HttpSession的实现,包括使用cookie的开关选项,使用URL重写的开关选项,session持久化的设置,session失效时间的设置,以及针对cookie的各种设置,比如设置cookie的名字、路径、域, cookie的生存时间等。
  一般情况下,session都是存储在内存里,当服务器进程被停止或者重启的时候,内存里的session也会被清空,如果设置了session的持久化特性,服务器就会把session保存到硬盘上,当服务器进程重新启动或这些信息将能够被再次使用, Weblogic Server支持的持久性方式包括文件、数据库、客户端cookie保存和复制。
  复制严格说来不算持久化保存,因为session实际上还是保存在内存里,不过同样的信息被复制到各个cluster内的服务器进程中,这样即使某个服务器进程停止工作也仍然可以从其他进程中取得session。
  cookie生存时间的设置则会影响浏览器生成的cookie是否是一个会话cookie。默认是使用会话cookie。有兴趣的可以用它来试验我们在第四节里提到的那个误解。
  cookie的路径对于web应用程序来说是一个非常重要的选项,Weblogic Server对这个选项的默认处理方式使得它与其他服务器有明显的区别。后面我们会专题讨论。
  HttpSession类它提供了setAttribute()和getAttribute()方法存储和检索对象。HttpSession提供了一个会话ID关键字,一个参与会话行为的客户端在同一会话的请求中存储和返回它。servlet引擎查找适当的会话对象,并使之对当前请求可用。HttpServletRequest 接口提供了以下方法来获取HttpSession实例。
  public HttpSession getSession() :该方法取得请求所在的会话。
  public HttpSession getSession(Boolean create):返回当前请求的会话。如果当前请求不属于任何会话,而且create参数为true,则创建一个会话,否则返回null。此后所有来自同一个的请求都属于这个会话,通过它的getSession返回的是当前会话。
  方法
  介绍
  public void setAttribute(String name,Object value)
  将value对象以name名称绑定到会话
  public object getAttribute(String name)
  取得name的属性值,如果属性不存在则返回null
  public void removeAttribute(String name)
  从会话中删除name属性,如果不存在不会执行,也不会抛处错误.
  public Enumeration getAttributeNames()
  返回和会话有关的枚举值
  public void invalidate()
  使会话失效,同时删除属性对象
  public Boolean isNew()
  用于检测当前客户是否为新的会话
  public long getCreationTime()
  返回会话创建时间
  public long getLastAccessedTime()
  返回在会话时间内web容器接收到客户最后发出的请求的时间
  public int getMaxInactiveInterval()
  返回在会话期间内客户请求的最长时间.秒
  public void setMasInactiveInterval(int seconds)
  允许客户客户请求的最长时间
  ServletContext getServletContext()
  返回当前会话的上下文环境,ServletContext对象可以使Servlet与web容器进行通信
  public String getId()
  返回会话期间的识别号
分享到:
评论

相关推荐

    javax.servlet.jar下载

    javax.servlet.http.HttpSession.class javax.servlet.http.HttpSessionContext.class javax.servlet.http.Cookie.class javax.servlet.http.HttpSessionAttributeListener.class javax.servlet....

    servlet-api.jar 适用于import javax.servlet.http.HttpSession;异常

    servlet-api.jar 适用于import javax.servlet.http.HttpSession;异常 直接下载后直接导入 即可,

    servlet-api-2.4.jar.zip

    javax.servlet.http.HttpSession javax.servlet.RequestDispatcher javax.servlet.SingleThreadModel javax.servlet.ServletInputStream javax.servlet.ServletContextEvent javax.servlet.ServletOutputStream ...

    servlet2.4doc

    getAttribute(String) - Method in interface javax.servlet.http.HttpSession Returns the object bound with the specified name in this session, or null if no object is bound under the name. ...

    【项目实战案例】java校园订餐系统项目(web端)

    import javax.servlet.http.HttpSession; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts....

    学习servlet的实例和参考api

    类 ServletConfig ServletOutputStream ServletContext ServletInputStream ServletResponse GenericServlet ServletRequest Servlet <br>javax.servlet.http<br>接口 类 HttpSession Cookie...

    比较简单的添加购物车,不过只有一个小程序

    import javax.servlet.http.HttpSession; import com.liunuy.bean.CartItem; import com.liunuy.bean.ShopCart; public class AddItemToShopCartAction extends HttpServlet { private static final long ...

    servlet-api.zip

    import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import javax.servlet.http.HttpServletRequest;...import javax.servlet.http.HttpSession; import javax.servlet.ServletException;

    jsp有哪些内置对象?作用分别是什么?分别有什么方法?

     session表示一个请求的javax.servlet.http.HttpSession对象。Session可以存贮用户的状态信息  applicaton 表示一个javax.servle.ServletContext对象。这有助于查找有关servlet引擎和servlet环境的信息  config...

    session机制详解

    目录: 一、术语session 二、HTTP协议与状态保持 三、理解cookie机制 四、理解session机制 五、理解javax.servlet.http.HttpSession 六、HttpSession常见问题 七、跨应用程序的session共享 八、总结

    新闻发布系统

    import javax.servlet.http.HttpSession; import org.apache.log4j.Logger; import org.news.entity.Topic; import org.news.service.TopicService; import org.news.service.impl.TopicServiceImpl; /** * 后台...

    项目:团购网站

    import javax.servlet.http.HttpSession; public class CheckAccount extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, ...

    jsp数据报表(柱状,饼状,折线)

    import javax.servlet.http.HttpSession; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartRenderingInfo; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import ...

    jsp 验证码 控件

    import javax.servlet.http.HttpSession; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class CodePicServlet extends HttpServlet { protected void ...

    springboot-家具销售电商平台lw+ppt

    springboot-家具销售电商平台lw+ppt 毕业设计 import javax.servlet.http.HttpServletRequest;...import javax.servlet.http.HttpSession; import java.util.List; @Controller public class LoginController

    JSP100选择题 JSP100选择题 适合考前练习

    运用javax.servlet.http.HttpServletRequest接口的sendRedirect方法 B.运用javax.servlet.http.HttpServletResponse接口的sendRedirect方法 C.运用javax.servlet.RequestDispatcher接口的forward方法 D.运用javax....

    Session-JavaWeb专题

    本课程系统地讲解了SESSION这个知识点,主要讲解了Session的...学习本课程可以理解Session机制,javax.servlet.http.HttpSession及HttpSession常见问题。免费试听地址:http://www.itcast.net/portal/courses/unit/124

    SimpleServlet.zip

    import javax.servlet.http.HttpSession; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet....

    java 同一用户不能同时登陆问题

    import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; import org.apache.struts2.ServletActionContext; import ...

    JAVA WEB 开发详解(JSP+Serlet)

    import javax.servlet.http.*; public class LoginCheckServlet extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException { ...

Global site tag (gtag.js) - Google Analytics