以前看到书上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. Libjingle 库

    Libjingle 是google talk voice(语音聊天) 和 p2p interoperability(点对点操作)库,是提供了google talk,p2p文件共享和语音呼叫能力的组件集 ...

  2. Javascript中的For循环

    在开发的过程中,遍历是一个经常遇到的.而for循环则是Javascript工具箱里一个好用的,也常用的工具.每个人的习惯不同,for循环的写法也不尽相同. 1.不写声明变量的写法: for(var i ...

  3. LeetCode(61) Rotate List

    题目 Given a list, rotate the list to the right by k places, where k is non-negative. For example: Giv ...

  4. 杭电 2803 The MAX(sort)

    Description Giving N integers, V1, V2,,,,Vn, you should find the biggest value of F.  Input Each tes ...

  5. 使用JS对form的内容验证失败后阻止提交

    1.form的两个事件 submit,提交表单,如果直接调用该函数,则直接提交表单 onSubmit,提交按钮点击时先触发,然后触发submit事件.如果不加控制的话,默认返回true,因此表单总能提 ...

  6. Java反射机制(Reflect)解析-----https://www.cnblogs.com/fzz9/p/7738381.html

    Java反射机制(Reflect)解析-----https://www.cnblogs.com/fzz9/p/7738381.html

  7. HDU 2280 状压DP

    用dfs找到状态的最优解 且那个正方形块可以由两个水平块组成,所以无需考虑 #include <cstdio> #include <cstring> #include < ...

  8. [NOIP2007] 提高组 洛谷P1005 矩阵取数游戏

    题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij均为非负整数.游戏规则如下: 1.每次取数时须从每行各取走一个元素,共n个.m次后取完矩阵所有元素: 2. ...

  9. 关于jquery stopPropagation()阻止冒泡事件

    我们经常会遇到点击两个或者多个重叠的层事件的时候,往往点击最里的的一层会接连触发外面的点击事件.这时候就需要用到stopPropagation事件即阻止冒泡事件html代码如下<!DOCTYPE ...

  10. 洛谷——P1832 A+B Problem(再升级)

    P1832 A+B Problem(再升级) 题目背景 ·题目名称是吸引你点进来的 ·实际上该题还是很水的 题目描述 ·1+1=? 显然是2 ·a+b=? 1001回看不谢 ·哥德巴赫猜想 似乎已呈泛 ...