以前看到书上session 的生命周期,知道session的生命周期是在第一次访(即打开浏览器输入地址成功访问)的时候被创建。同时HttpSessionListener接口的sessionCreate会被调用。

等到浏览器关闭或者服务器重启的时候session会被销毁。

但在最近的实验中发现,在浏览器直接访问默认的servlet时,session并没有被创建出来。而当在servlet中执行    HttpSession session = request.getSession();     时显示session被创建出来。

由此可见浏览器访问时session 并不是一定会被创建的。

查api可以发现这么一段:

getSession
HttpSession getSession(boolean create) Returns the current HttpSession associated with this request or, if there is no current session and create is true, returns a new session.
If create is false and the request has no valid HttpSession, this method returns null. To make sure the session is properly maintained, you must call this method before the response is committed. If the container is using cookies to maintain session integrity and is asked to create a new session when the response is committed, an IllegalStateException is thrown.
Parameters:create - true to create a new session for this request if necessary; false to return null if there's no current sessionReturns:the HttpSession associated with this request or null if create is false and the request has no valid sessionSee Also:getSession() getSession
HttpSession getSession() Returns the current session associated with this request, or if the request does not have a session, creates one.
Returns:the HttpSession associated with this requestSee Also:getSession(boolean)

划横线部分是说如果create为false 并且request 内没有HttpSession 对象  ,此方法会返回null 说明session默认是不存在的,调用getsession()才会被创建。

而对于jsp中平常以为session在客户端访问时就被创建,然而事实是直到servlet端程序调用HttpServletRequest.getSession(true)这样的语句时才被创建,想到jsp来自于servlet就不难理解,如果jsp没有显式的使用 <% @page session="false"%> 关闭session ,jsp文件在编译成servlet时会自动加上HttpSession session = HttpServletRequest.getSession(true);     这也是jsp中隐含对象session的来历。

session是服务器端的,浏览器是客户端的,浏览器的关闭和session是没有关系的,session失效基本是以下三种情况:

  1. 超时
  2. 手动注销( session.invalidate();  )
  3. 服务器重启

session 生命周期的更多相关文章

  1. session生命周期

    session生命周期 原文链接:http://blog.sina.com.cn/s/blog_72c8c1150100qpgl.html 文中黄色字体为我的标记修改或添加 Session保存在服务器 ...

  2. php session 生命周期代码实例

     php session 生命周期代码实例        我们为什么需要Session,就是因为我们需要存储各个用户的状态数据.那么试问,如果由你来设计解决这个需求的方案,那么也许你会设置这样一个数据 ...

  3. JAVA-JSP内置对象之session对象设置并获得session生命周期

    相关资料:<21天学通Java Web开发> session对象设置并获得session生命周期1.通过session对象的setMaxInactiveInterval()方法可以设置se ...

  4. session生命周期,与cookie的区别

    sessinon在用户访问第一次访问服务器时创建. Session什么时候失效? 1. 服务器会把长时间没有活动的Session从服务器内存中清除,此时Session便失效.Tomcat中Sessio ...

  5. session生命周期(一)

    Session存储在服务器端,一般为了防止在服务器的内存中(为了高速存取),Session在用户访问第一次访问服务器时创建,需要注意只有访问JSP.Servlet等程序时才会创建Session,只访问 ...

  6. php会话(session)生命周期概念介绍及设置更改和回收

    http://www.169it.com/article/8429580816135935852.html https://my.oschina.net/jiec/blog/227252  sessi ...

  7. Request Session生命周期及struts1 中service的编写

    现在接手的项目是一个早期的struts1框架的项目.同时也是刚开始接触web 以及struts1架构. 在处理多个action时,有一个tab子页面需要每5s自动刷新一次. 然后在测试过程中发现,点击 ...

  8. Session生命周期讨论

    文章级别:Java初级    预备技能点:JSP内置对象, 监听器, 序列化           在程序开发的时候, request session appplication内置对象, 是用的比较多的 ...

  9. springboot处理session生命周期

    在使用springboot开发过程中发现用户登陆后60s后session就自动失效了,需要重新登陆,明明 application.yml  文件里已经配置了 server.session.timeou ...

  10. JavaWeb关于session生命周期的几种设置方法

    一般session的生命周期都是建立在用户登录系统后对用户信息进行一个记录,session类似于你有一张银行卡,而卡里的钱就是属于session存储的信息,卡掉了就不能取出里面的钱. 以前sessio ...

随机推荐

  1. 简单说一下Set,list,Map的类型和自己的特点

    首先是Set,List,Map Set和list都是继承了Conllection接口,而Map是本身就是一个接口 set是最简单的一种集合,没有重复对象 set接口主要有两个实现: 1,hashSet ...

  2. impdp导入

    //导入命令impdp 用户/密码@数据库链接 directory=数据库中映射路径 schemas=原用户 remap_schema=原用户:现用户 remap_tablespace=原表空间:现表 ...

  3. 数据库 SQL SQL转义

    SQL转义 @author ixenos 前言 类似文件分隔符在不同系统的实现不同,我们需要一个中间的转义字符来作为接口,各厂商再具体实现 而SQL的转义语法主要为了支持各种数据库普遍支持的特性,但各 ...

  4. hdu 1828 Picture(线段树,扫描线)

    A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wa ...

  5. hdu 2433 Travel (最短路树)

     One day, Tom traveled to a country named BGM. BGM is a small country, but there are N (N <= 100) ...

  6. Web App 响应式页面制作 笔记整理

    一.移动端种类.分辨率大小 说明: 以主流的iPad.iPhone为例. 工具: Resizer官网: Resizer 用法: 将通栏处写有 “Click or Bookmark”的蓝色按钮拖拽至标签 ...

  7. nyoj_176_队花的烦恼二_201404262008

    队花的烦恼二 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 ACM队队花C小+最近在X大OJ上做题,竟发现了一道做不出来的…水题!她快郁闷死了……也许是最近状态不太 ...

  8. Core java for impatient 笔记 ch8 流

    流stream 使用了数据视图,让你可以在比集合更高的概念上指定操作使用流,你只需要将操作的调度留给实现,例如,假设你要计算某个属性的平均值,你只需要指定数据源和属性,然后流类库会优化计算,比如使用多 ...

  9. POJ1328 Radar Installation 解题报告

    Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...

  10. How to force immediate stop of threads in Jmeter servers如何在jmeter执行完,立即停止jmeter

    https://stackoverflow.com/questions/38900315/how-to-force-immediate-stop-of-threads-in-jmeter-server ...