Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 1011|回复: 0

[jsp学习]对request.getSession(false)的理解

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-2 06:45:55 | 显示全部楼层 |阅读模式
    【前面的话】
    在网上经常看到有人对request.getSession(false)提出疑问,我第一次也很迷惑,看了一下J2EE API,看一下官网是怎么解释的。
    【官方解释】

    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】:
    我周围很多同事是这样写的;
      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存在的情况,正常的判断应该是这样:

    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)方法,看看高手写的源码吧:哈哈。。


    复制代码 代码如下:



    /**

    * 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是否为空,若为空就抛异常。
    回复

    使用道具 举报

    该用户从未签到

    发表于 2015-9-15 11:19:14 | 显示全部楼层
    这个在编码中会使用到,解释的非常清楚,学习了
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-25 00:58 , Processed in 0.370626 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表