getSession() , getSession(false) ,getSession(true) 区别

来源:互联网 发布:python 小脚本实例 编辑:程序博客网 时间:2024/06/06 02:22

http://www.mimul.com/pebble/default/2006/07/20/1153394700000.html


 getSession() ,getSession(true) , getSession(false) 区别


1. getSession(), getSession(true)

 - 如果HttpSession存在 就会返还 HttpSession  如果没有 就新生成一个Session


2. getSession(false)

 - 如果HttpSession存在 就会返还 HttpSession  如果没有 返还值为null


3. Example
HttpSession session = request.getSession();
HttpSession session = request.getSession(true);

上结果一至


想确认是否是新的session 可以用 session.isNew() 来判断


false 使用时要注意的 一点  返还值可能为NULL  所以要多检查一次

HttpSession session = request.getSession(false);
if (session != null)
    User user = (User) session.getAttribute("User");
原创粉丝点击