request.getSession(true) request.getSession(false) request.getSession()
2017-04-25 10:39
447 查看
request.getSession(true):若存在会话则返回该会话,否则新建一个会话。
request.getSession(false):若存在会话则返回该会话,否则返回NULL(false):
request.getSession():和getSession(true)相同
使用
当向Session中存取登录信息时,一般建议:HttpSession
session =request.getSession();
当从Session中获取登录信息时,一般建议:HttpSession
session =request.getSession(false);
更简洁的方式
如果你的项目中使用到了Spring(当然大点的项目都用到了),对session的操作就方便多了。如果需要在Session中取值,可以用WebUtils工具(org.springframework.web.util.WebUtils)的getSessionAttribute(HttpServletRequestrequest,
String name)方法,看看源码:
request.getSession(false):若存在会话则返回该会话,否则返回NULL(false):
request.getSession():和getSession(true)相同
使用
当向Session中存取登录信息时,一般建议:HttpSession
session =request.getSession();
当从Session中获取登录信息时,一般建议:HttpSession
session =request.getSession(false);
更简洁的方式
如果你的项目中使用到了Spring(当然大点的项目都用到了),对session的操作就方便多了。如果需要在Session中取值,可以用WebUtils工具(org.springframework.web.util.WebUtils)的getSessionAttribute(HttpServletRequestrequest,
String name)方法,看看源码:
publicstatic Object getSessionAttribute(HttpServletRequest request, String name){ Assert.notNull(request, "Request must not be null"); HttpSession session =request.getSession(false); return (session != null ?session.getAttribute(name) : null); }
User user = (User)WebUtils.getSessionAttribute(request, “user”);
相关文章推荐
- request.getSession()和request.getSession(true/false)的理解
- request.getSession(false),request.getSession()和request.getSession(true)的区别
- request.getSession(true)和request.getSession(false)的区别
- getSession(true)和getSession(false)的区别
- request.getSession(false)与request.getSession(true)区别
- request.getSession(true)和request.getSession(false)的区别
- request.getSession(false)&request.getSession(true)的区别
- request.getSession(false)与request.getSession(true)区别
- request.getSession(true)和request.getSession(false)的区别
- request.getSession()和request.getSession(true/false)的理解
- request.getSession(true)和request.getSession(false)的区别
- request.getSession(true)和request.getSession(false)的区别
- request.getSession() 与 request.getSession(true/false)有何区别?
- request.getSession(true)和request.getSession(false)的区别
- 有关request.getSession参数(true或者false)的含义
- 关于request.getSession(true/false/null)的区别
- request.getSession(false)与request.getSession(true)的区别
- 有关request.getSession参数(true或者false)的含义
- getSession() , getSession(false) ,getSession(true) 区别
- 【转】于request.getSession(true/false/null)的区别