session会话技术

session工作原理图

 session 小结:

① session是存在服务器的内存中

② 一个用户浏览器,独享一个session域对象

③ session中的属性的默认生命周期30min,你可以通过 web.xml来修改

sessioncookie生命周期小结:

 3session生命周期的设置

1一个地方是 tomcat/conf/web.xml

  <session-config>

        <session-timeout>30</session-timeout>//表示30分钟的意思

</session-config>

对所有的web应用生效

2另外一个地方,就是在单个web应用的下去修改web.xml

<session-config>

  <session-timeout>30</session-timeout>session精确到分钟,cookie精确到秒

  </session-config>

如果发生冲突,则以自己的web应用优先级高

3session.setMaxInactiveinterval(60)发呆六十秒后session失效


sessioncookie生命周期小结:

① session中可以存放多个属性

② session 可以存放对象

③ 如果 session.setAttribute(“name”,val) , 如果名字重复,则会替换该属性.

 

 session的更深入理解:

为什么服务器能够为不同的浏览器提供不同session

因为每个浏览器去访问web站点的时候,如果发出的http请求头没有带JSESSIONID头就会自动给你创建一个并返回


控制session的销毁时间

session的销毁时间的讨论借助一个案例:

面试题: (应用:关掉IE后,再开IE,上次购买的商品还在。->涉及到session销毁时间)

 

分析

我们的session 生命周期如果是30min,session不会随浏览器的关闭,而自动销毁.而会到30min后,才会被服务器销毁.

我们使用代码来实现该功能(session + cookie结合使用)

 

分析实现的思路:

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

 

response.setContentType("text/html;charset=utf-8");

PrintWriter out = response.getWriter();

HttpSession session = request.getSession();

session.setAttribute("name", "张辉胤");

out.println("创一个session并放入姓名属性");

Cookie cookie = new Cookie("JSESSIONID", session.getId());

cookie.setMaxAge(60*30);

response.addCookie(cookie);

}

 

 

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

 

response.setContentType("text/html;charset=utf-8");

PrintWriter out = response.getWriter();

HttpSession httpSession = request.getSession();

String name = (String) httpSession.getAttribute("name");

out.println("name = "+name);

}

 

u ie禁用cookie后使用session的方法


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值