Session的创建和设置

1、Session的获取:

(1)无参的方法:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        HttpSession httpSession=request.getSession();
        System.out.println(httpSession.getId());
    }

 

 请求中无Cookie,但是响应中存在Cookie:

 

 

 

 当再次访问该Servlet的时候,请求中存在Cookie,响应中的Cookie已经没有了:

 

 

 

以上为无参的方法获取Session,如果没有Session则创建一个,如果有则直接返回。

(2)有参的方法:

参数为false:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        HttpSession httpSession=request.getSession(false);
        System.out.println(httpSession.getId());
    }

如果有Session则直接返回。

没有的话返回500错误:

 

 参数为true:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        HttpSession httpSession=request.getSession(true);
        System.out.println(httpSession.getId());
    }

此方法与不加参数等效。

 2、Session的有效期限:

 

 前三次访问是连续访问三次CookieServlet,可以看出,SESSIONID的值是不会发生变化的,但是当关闭了浏览器,第四次访问CookieServlet时,SESSIONID发生了变化;第五次为更换了浏览器之后的结果,SESSIOID依旧会发生变化。

以下情况下Session需要重新建立:

(1)用户关闭了浏览器。

(2)关闭了服务器。

(3)用户没有向服务器提出请求(超过30分钟),过期后服务器自动删除,从不操作服务端资源开始计时。

 

       可以修改(直接修改或在自己的web.xml中配置,将默认的时间覆盖掉)。

 

3、Session的设置:

(1)时间:

   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        HttpSession httpSession=request.getSession(true);
        httpSession.setMaxInactiveInterval(10);//十秒后失效
        System.out.println(httpSession.getId());
    }

第一次访问,成功返回SESSIONID。

 

 过十几秒钟后重新访问发现SESSIONID的值已经改变了:

 

 这是因为第一个SESSIOID已经过期了,需要创建第二个。

(2)强制失效(手动销毁):

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        HttpSession httpSession=request.getSession(true);
        httpSession.invalidate();
        System.out.println(httpSession.getId());
    }

 

 即执行invalidate()后可以将创建的SESSION立即结束。

4、session的特点:

(1)存储在服务器端。

(2)依赖于Cookie,借助Cookie存储JSESSIONID。

(3)存在有效期限。

 

转载于:https://www.cnblogs.com/zhai1997/p/11558649.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值