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

本文解析了HTTP请求中getSession方法的工作原理,特别是对getSession(false)的行为进行了深入探讨,并提供了最佳实践建议及Spring框架下的简化操作。

本文属于本人原创,转载请注明出处:http://blog.csdn.net/xxd851116/archive/2009/06/25/4296866.aspx

【前面的话】

在网上经常看到有人对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】:

我周围很多同事是这样写的;

需要注意的地方是request.getSession() 等同于 request.getSession(true),除非我们确认session一定存在或者sesson不存在时明确有创建session的需要,否则尽量使用request.getSession(false)。在使用request.getSession()函数,通常在action中检查是否有某个变量/标记存放在session中。这个场景中可能出现没有session存在的情况,正常的判断应该是这样:


【投机取巧】:

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

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

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

  

### `request.getSession(true)` 与 `request.getSession(false)` 的区别 在 Java Web 开发中,`request.getSession(true)` 和 `request.getSession(false)` 是 `HttpServletRequest` 接口中用于获取 `HttpSession` 对象的两种方式,它们在行为上存在关键差异。 当调用 `request.getSession(true)` 时,如果当前请求中没有与之关联的 `HttpSession`,则会自动创建一个新的 `HttpSession` 对象并返回。这种行为适用于需要确保存在一个会话的场景,例如用户登录后需要将用户信息存储到会话中: ```java HttpSession session = request.getSession(true); session.setAttribute("user", user); ``` 这种调用方式等价于直接调用 `request.getSession()`,因为默认参数为 `true`,即自动创建新会话[^1]。 而调用 `request.getSession(false)` 时,如果当前请求中没有关联的 `HttpSession`,则不会创建新的会话,而是返回 `null`。这种行为适用于仅需读取会话信息而不希望在无会话时自动创建的场景,例如在受保护资源中验证用户是否已登录: ```java HttpSession session = request.getSession(false); if (session != null) { User user = (User) session.getAttribute("user"); if (user != null) { // 用户已登录 } else { // 用户未登录或会话失效 } } else { // 不存在会话 } ``` 使用 `false` 参数可以避免不必要的会话创建,从而节省服务器资源并提升性能。此外,这种方式还能帮助开发者更清晰地处理未登录或会话失效的情况,避免出现空指针异NullPointerException)[^3]。 ### 适用场景 - **`request.getSession(true)`**:适用于需要确保存在一个会话的场景,例如用户登录、初始化会话数据等。 - **`request.getSession(false)`**:适用于仅需读取会话数据而不希望自动创建会话的场景,例如访问受保护资源前的会话验证。 通过合理选择参数,开发者可以更精细地控制会话的生命周期和资源使用情况,提升应用的安全性和效率。 --- ###
评论 30
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值