request.getSession()和request.getSession(true/false)的理解
2015-11-11 21:13
471 查看
request.getSession()和request.getSession(true)意思相同:获取session,如果session不存在,就新建一个
reqeust.getSession(false)获取session,如果session不存在,则返回null
如果 项目中无法确定回话一定存在,最好用request.session(false);
getSession(boolean create)意思是返回当前reqeust中的HttpSession ,如果当前reqeust中的HttpSession 为null,当create为true,就创建一个新的Session,否则返回null;
简而言之:
HttpServletRequest.getSession(ture)等同于 HttpServletRequest.getSession()
HttpServletRequest.getSession(false)等同于 如果当前Session没有就为null;
3. 使用
当向Session中存取登录信息时,一般建议:HttpSession session =request.getSession();
当从Session中获取登录信息时,一般建议:HttpSession session =request.getSession(false);
4. 更简洁的方式
如果你的项目中使用到了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);
}
注:Assert是Spring工具包中的一个工具,用来判断一些验证操作,本例中用来判断reqeust是否为空,若为空就抛异常
你使用时:WebUtils.setSessionAttribute(request, “user”, User);
User user = (User)WebUtils.getSessionAttribute(request, “user”);
三、运行结果
以上例子均测试验证通过。
reqeust.getSession(false)获取session,如果session不存在,则返回null
如果 项目中无法确定回话一定存在,最好用request.session(false);
getSession(boolean create)意思是返回当前reqeust中的HttpSession ,如果当前reqeust中的HttpSession 为null,当create为true,就创建一个新的Session,否则返回null;
简而言之:
HttpServletRequest.getSession(ture)等同于 HttpServletRequest.getSession()
HttpServletRequest.getSession(false)等同于 如果当前Session没有就为null;
3. 使用
当向Session中存取登录信息时,一般建议:HttpSession session =request.getSession();
当从Session中获取登录信息时,一般建议:HttpSession session =request.getSession(false);
4. 更简洁的方式
如果你的项目中使用到了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);
}
注:Assert是Spring工具包中的一个工具,用来判断一些验证操作,本例中用来判断reqeust是否为空,若为空就抛异常
你使用时:WebUtils.setSessionAttribute(request, “user”, User);
User user = (User)WebUtils.getSessionAttribute(request, “user”);
三、运行结果
以上例子均测试验证通过。
相关文章推荐
- muduo库阅读(33)——Net部分:定时器队列TimerQueue
- UITableViewEdit
- UITableView的一些方法
- 实现UIScrollView循环滚动
- iOS学习笔记--如何为UITextView设置placeholder
- SoapUI启动报错:The JVM could not be started. The maximum heap size (-Xmx) might be too large
- UIbottun使用详解
- KVC中setValuesForKeysWithDictionary:(写的不错)
- Quick中require与import的区别
- QuickSort
- UGUI之滑动翻页效果
- android view requestLayout,invalidate 事件的分发,接收,以及处理
- 优化UITableViewCell高度计算的那些事
- Ueditor绝对路径问题
- 如何让 UITableView 的 headerView跟随 cell一起滚动
- Builder模式
- LightOJ 1369 - Answering Queries(规律)
- iOS笔记UI--ipad分页控件
- UE4 Android Device Compatibility
- UE4.9 Hardware & Software Specifications