对request.getSession(false)的理解
2016-04-19 15:36
357 查看
转自:http://blog.csdn.net/xxd851116/article/details/4296866
【前面的话】
在网上经常看到有人对request.getSession(false)提出疑问,我第一次也很迷惑,看了一下J2EE1.3 API,看一下官网是怎么解释的。
【官方解释】
getSession
public HttpSession getSession(boolean create)
Returns the current
If
To make sure the session is properly maintained, you must call this method before the response is committed. If the Container is
using cookies to maintain session integrity and is asked to create a new session when the response is committed, an IllegalStateException is thrown.
Parameters:
Returns: the
译:
getSession(boolean create)意思是返回当前reqeust中的HttpSession ,如果当前reqeust中的HttpSession 为null,当create为true,就创建一个新的Session,否则返回null;
简而言之:
HttpServletRequest.getSession(ture) 等同于 HttpServletRequest.getSession()
HttpServletRequest.getSession(false) 等同于 如果当前Session没有就为null;
【问题和bug】:
我周围很多同事是这样写的;
[java] view
plain copy
HttpSession session = request.getSession(); // a new session created if no session exists, 哈哈!完蛋啦!如果session不存在的话你又创建了一个!
String user_name = session.getAttribute("user_name");
需要注意的地方是request.getSession() 等同于 request.getSession(true),除非我们确认session一定存在或者sesson不存在时明确有创建session的需要,否则尽量使用request.getSession(false)。在使用request.getSession()函数,通常在action中检查是否有某个变量/标记存放在session中。这个场景中可能出现没有session存在的情况,正常的判断应该是这样:
[java] view
plain copy
HttpSession session = request.getSession(false);
if (session != null) {
String user_name = session.getAttribute("user_name");
}
【投机取巧】:
如果项目中用到了spring(其实只要是Java的稍大的项目,Spring是一个很好的选择),对session的操作就方便多了。如果需要在Session中取值,可以用WebUtils工具(org.springframework.web.util.WebUtils)的getSessionAttribute(HttpServletRequest
request, String name)方法,看看高手写的源码吧:哈哈。。
[java] view
plain copy
/**
* Check the given request for a session attribute of the given name.
* Returns null if there is no session or if the session has no such attribute.
* Does not create a new session if none has existed before!
* @param request current HTTP request
* @param name the name of the session attribute
* @return the value of the session attribute, or <code>null</code> if not found
*/
public static 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是否为空,若为空就抛异常。
上面的代码又可以简洁一下啦,看吧:
[java] view
plain copy
HttpSession session = request.getSession(false);
String user_name = WebUtils.getSessionAttribute(reqeust, "user_name");
【前面的话】
在网上经常看到有人对request.getSession(false)提出疑问,我第一次也很迷惑,看了一下J2EE1.3 API,看一下官网是怎么解释的。
【官方解释】
getSession
public HttpSession getSession(boolean create)
Returns the current
HttpSessionassociated with this request or, if if there is no current session and
createis true, returns a new session.
If
createis
falseand the request has no valid
HttpSession, this method returns
null.
To make sure the session is properly maintained, you must call this method before the response is committed. If the Container is
using cookies to maintain session integrity and is asked to create a new session when the response is committed, an IllegalStateException is thrown.
Parameters:
true- to create a new session for this request if necessary;
falseto return
nullif there's no current session
Returns: the
HttpSessionassociated with this request or
nullif
createis
falseand the request has no valid session
译:
getSession(boolean create)意思是返回当前reqeust中的HttpSession ,如果当前reqeust中的HttpSession 为null,当create为true,就创建一个新的Session,否则返回null;
简而言之:
HttpServletRequest.getSession(ture) 等同于 HttpServletRequest.getSession()
HttpServletRequest.getSession(false) 等同于 如果当前Session没有就为null;
【问题和bug】:
我周围很多同事是这样写的;
[java] view
plain copy
HttpSession session = request.getSession(); // a new session created if no session exists, 哈哈!完蛋啦!如果session不存在的话你又创建了一个!
String user_name = session.getAttribute("user_name");
需要注意的地方是request.getSession() 等同于 request.getSession(true),除非我们确认session一定存在或者sesson不存在时明确有创建session的需要,否则尽量使用request.getSession(false)。在使用request.getSession()函数,通常在action中检查是否有某个变量/标记存放在session中。这个场景中可能出现没有session存在的情况,正常的判断应该是这样:
[java] view
plain copy
HttpSession session = request.getSession(false);
if (session != null) {
String user_name = session.getAttribute("user_name");
}
【投机取巧】:
如果项目中用到了spring(其实只要是Java的稍大的项目,Spring是一个很好的选择),对session的操作就方便多了。如果需要在Session中取值,可以用WebUtils工具(org.springframework.web.util.WebUtils)的getSessionAttribute(HttpServletRequest
request, String name)方法,看看高手写的源码吧:哈哈。。
[java] view
plain copy
/**
* Check the given request for a session attribute of the given name.
* Returns null if there is no session or if the session has no such attribute.
* Does not create a new session if none has existed before!
* @param request current HTTP request
* @param name the name of the session attribute
* @return the value of the session attribute, or <code>null</code> if not found
*/
public static 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是否为空,若为空就抛异常。
上面的代码又可以简洁一下啦,看吧:
[java] view
plain copy
HttpSession session = request.getSession(false);
String user_name = WebUtils.getSessionAttribute(reqeust, "user_name");
相关文章推荐
- Andorid学习笔记(ADT)-常见的UI布局
- [poj 1947] Rebuilding Roads 树形DP
- leetcode 303. Range Sum Query - Immutable(前缀和)
- [置顶] 开源框架GPUImage 的简单说明
- 用户设计VS用户体验(UI,UX干货)
- Access restriction: The type BASE64Encoder is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar
- PHP中require和include路径问题总结
- AndroidUI之三分钟教你实现效果简单大气的Dialog提示框
- UIImage 内存细节
- UIImage 内存细节
- break,continue,break的用法与区别
- leetcode第一刷_Unique Binary Search Trees
- Android Wear SDK 中文 ---- 创建表盘服务 [Building a Watch Face Service]
- Rabbit-service Message queue MQ 验证 校验
- UIEdgeInsets - 1
- UISearchBar学习笔记
- 跟王老师学Java三大特性(四):案例 QuickHit:玩家玩游戏
- 跟王老师学Java三大特性(二):案例 QuickHit:游戏输出字符串
- iOS UITextField限制字数
- 跟王老师学Java三大特性(一):案例 QuickHit:需求分析