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

【前面的话】

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

【官方解释】

  getSession 

public HttpSessiongetSession(boolean create)

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

If create is false and the request has no validHttpSession, 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 ornull 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  print?
  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  print?
  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  print?
  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  print?
  1. HttpSession session = request.getSession(false);
  2. String user_name = WebUtils.getSessionAttribute(reqeust, "user_name");

转载于:https://www.cnblogs.com/meishibiexuejava/p/8563864.html

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

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

    出处:http://blog.csdn.net/xxd851116/archive/2009/06/25/4296866.aspx [前面的话] 在网上经常看到有人对request.getSessio ...

  2. 27岁程序员转职赏金猎人:一个漏洞10万美元,比工资香多了

    Python实战社群 Java实战社群 长按识别下方二维码,按需求添加 扫码关注添加客服 进Python社群▲ 扫码关注添加客服 进Java社群▲ 作者 | Sambodhi 编辑 | Tina 来源 ...

  3. Python程序员常犯的十个错误

    不管是在学习还是工作过程中,人都会犯错.虽然Python的语法简单.灵活,但也一样存在一些不小的坑,一不小心,不管是初学者还是资深Python程序员都有可能会栽跟头. 常见错误1:错误地将表达式作为函 ...

  4. request.getSession(false)到底返回什么

    HttpSession session = request.getSession(false); 很明显传false如果session不存在返回Null.

  5. 读书笔记系列--《理解专业程序员》tips

    理解专业程序员,给您带来大师的tips *"什么东西是绿的,有轮子,而且长在房子的周围"?* *"猜不出来,答案呢"?* *"是草,轮子是我瞎说的&q ...

  6. OSChina 周一乱弹 —— 最无法理解的程序员行为

    2019独角兽企业重金招聘Python工程师标准>>> Osc乱弹歌单(2018)请戳(这里) [今日歌曲] @小鱼丁:分享陈珊妮的单曲<情歌> <情歌>- ...

  7. 从实践理解《程序员的自我修养》(1)

    从实践理解<程序员的自我修养>(1) 前言 这篇文档主要从实践的角度充分理解<程序员的自我修养>一书中提到的细节.书中提到的各种机制.数据结构,我都将在实际系统中找到并理解它们 ...

  8. 《理解专业程序员》读书笔记

    ​软件领域的经典,没有理由不站在巨人的肩膀上.每次遇到瓶颈,需要借助外力突破时,书籍,尤其是经典的好书,让人如虎添翼! | 一点提问: 专业程序员是怎么练成的? 如何培养使用工具的专业技术领导者? 为 ...

  9. 【知乎】怎么成为一个优秀的程序员,而不是一个优秀的码农?

    怎么成为一个优秀的程序员,而不是一个优秀的码农? 9 条评论 分享 默认排序按时间排序 98 个回答 3844赞同反对,不会显示你的姓名 萧井陌 微信公众号:炼瓜研究所 技术社区 - 3844 人赞同 ...

最新文章

  1. dubbo扫描第三方包_今天来浅谈一下dubbo
  2. 解决 echarts柱状图x轴数据隔一个显示
  3. synamic-datasource-spring-boot-starter实现动态数据源Mysql和Sqlserver
  4. 为什么选择Nginx
  5. 小米笔记本服务器系统,小米笔记本Pro GTX版
  6. Hadoop Mapreduce分区、分组、二次排序过程详解
  7. cogs 167. [USACO Mar07] 月度花费
  8. python logger_Python:logging 的巧妙设计!
  9. predict函数 R_R包randomForest的随机森林回归模型以及对重要变量的选择
  10. usb4java android,USB audio on Android platform
  11. 量子计算机代表人物,量子力学究竟“可怕”在哪?科学家的怀疑或许是对的
  12. 经典三层模型制作学校论坛系统(BBS)
  13. 全球及中国基因组学软件行业发展动态及前景趋势预测报告(2022-2027)
  14. 500MHz频率源设计(西电通院高频大作业)
  15. 大数据与传统数仓的区别?
  16. Cordova+Vue实现Android APP开发
  17. linux 7 查看网卡配置文件,如何查询centos网卡配置文件
  18. 短视频技术指南:国内最牛5家短视频解决方案提供商评测
  19. Jupyter Notebook中使用conda配置的Python虚拟环境
  20. Vue中根据输入的身份证号识别年龄、性别

热门文章

  1. 将字符串中的指定字符全局替换
  2. Java8-Guava实战示例
  3. 【概率与期望】[UVA11021]Tribles
  4. hdu 4292 Food 最大流
  5. 使用无锁的方式和有锁的方式的程序性能对比
  6. hbase+hive应用场景
  7. [转]JQuery.Ajax之错误调试帮助信息
  8. 认识Mahout下的云计算机器学习
  9. c语言有一个正整数加上100,c语言编程实现:一个整数,它加上100后是完全平方数,再加168又是完全平方数,求该数。...
  10. stm32f407能跑linux吗_跑步能跑进医院?那我该做跑步运动吗?想健康一点太难了...