对request.getSession(false)的理解(附程序员常疏忽的一个漏洞)

简介: 转自:http://blog.csdn.net/xxd851116/article/details/4296866【前面的话】在网上经常看到有人对request.getSession(false)提出疑问,我第一次也很迷惑,看了一下J2EE1.3 API,看一下官网是怎么解释的。

转自:http://blog.csdn.net/xxd851116/article/details/4296866

【前面的话】

在网上经常看到有人对request.getSession(false)提出疑问,我第一次也很迷惑,看了一下J2EE1.3 API,看一下官网是怎么解释的。 

【官方解释】

  getSession 

public HttpSession getSession(boolean create)

Returns the current HttpSession associated with this request or, if if there is no current session and create is true, returns a new session.

If create is false and 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; false to return null if there's no current session

Returns: the HttpSession associated with this request or null if create is false and 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
  1. HttpSession session = request.getSession();   // a new session created if no session exists, 哈哈!完蛋啦!如果session不存在的话你又创建了一个!  
  2. 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
  1. HttpSession session = request.getSession(false);  
  2. if (session != null) {  
  3.     String user_name = session.getAttribute("user_name");  
  4. }  


【投机取巧】:

如果项目中用到了Spring(其实只要是Java的稍大的项目,Spring是一个很好的选择),对session的操作就方便多了。如果需要在Session中取值,可以用WebUtils工具(org.springframework.web.util.WebUtils)的getSessionAttribute(HttpServletRequest request, String name)方法,看看高手写的源码吧:哈哈。。

[java]  view plain copy
  1. /** 
  2.  * Check the given request for a session attribute of the given name. 
  3.  * Returns null if there is no session or if the session has no such attribute. 
  4.  * Does not create a new session if none has existed before! 
  5.  * @param request current HTTP request 
  6.  * @param name the name of the session attribute 
  7.  * @return the value of the session attribute, or <code>null</code> if not found 
  8.  */  
  9. public static Object getSessionAttribute(HttpServletRequest request, String name) {  
  10.     Assert.notNull(request, "Request must not be null");  
  11.     HttpSession session = request.getSession(false);  
  12.     return (session != null ? session.getAttribute(name) : null);  
  13. }  

注:Assert是Spring工具包中的一个工具,用来判断一些验证操作,本例中用来判断reqeust是否为空,若为空就抛异常。

上面的代码又可以简洁一下啦,看吧:

[java]  view plain copy
  1. HttpSession session = request.getSession(false);  
  2. String user_name = WebUtils.getSessionAttribute(reqeust, "user_name");  
相关文章
|
SQL 关系型数据库 MySQL
总结 vue3 的一些知识点:MySQL 连接的使用
总结 vue3 的一些知识点:MySQL 连接的使用
|
NoSQL Java Redis
Java中使用RedisTemplate根据前缀获取key列表
我们在使用 Redis 的时候,会需要获取以某个字符串开头的所有 key
1128 0
|
存储 缓存 自然语言处理
ElasticSearch原理篇
介绍ElasticSearch的原理、集群等
5963 0
ElasticSearch原理篇
|
11月前
|
jenkins 测试技术 持续交付
Docker最佳实践:构建高效的CI/CD流水线
【10月更文挑战第17天】在现代软件开发实践中,持续集成(Continuous Integration, CI)和持续部署(Continuous Deployment, CD)已成为提高开发效率和软件质量的重要手段。Docker作为一种容器技术,为构建一致且隔离的开发环境提供了强有力的支撑。本文将探讨如何利用Docker来优化CI/CD流程,包括构建环境的标准化、镜像管理以及与CI/CD工具(如Jenkins、GitLab CI)的集成。
432 5
6、Mybatis-Plus wrapper的使用
这篇文章详细介绍了Mybatis-Plus中Wrapper的使用,包括QueryWrapper和UpdateWrapper的基本概念、组装查询、排序、删除、修改条件的方法,以及如何设置条件优先级、组装SELECT子句和实现子查询等高级用法。
|
9月前
一文带你封装Vue3 Echarts
一文带你封装Vue3 Echarts
831 7
一文带你封装Vue3 Echarts
|
存储 人工智能 算法
AI与大数据的结合:案例分析与技术探讨
【8月更文挑战第22天】AI与大数据的结合为各行各业带来了前所未有的机遇和挑战。通过具体案例分析可以看出,AI与大数据在电商、智能驾驶、医疗等领域的应用已经取得了显著成效。未来,随着技术的不断进步和应用场景的不断拓展,AI与大数据的结合将继续推动各行业的创新与变革。
|
开发框架 前端开发 JavaScript
在Vue3+TypeScript 前端项目中使用事件总线Mitt
在Vue3+TypeScript 前端项目中使用事件总线Mitt
HTML【详解】表格 table 标签(table的属性,语义化表格,简易表格,合并单元格)
HTML【详解】表格 table 标签(table的属性,语义化表格,简易表格,合并单元格)
559 0
HTML【详解】表格 table 标签(table的属性,语义化表格,简易表格,合并单元格)
|
数据采集 数据挖掘 数据处理
Pandas在Python面试中的应用与实战演练
【4月更文挑战第16天】本文介绍了Python数据分析库Pandas在面试中的常见问题和易错点,包括DataFrame和Series的创建、数据读写、清洗预处理、查询过滤、聚合分组、数据合并与连接。强调了数据类型检查、索引理解、避免过度使用循环、内存管理和正确区分合并与连接操作的重要性。通过掌握这些知识和代码示例,可提升面试者在Pandas方面的专业能力。
611 3