java 中 request.getSession(true、false、null)的区别

 更新时间:2017年02月17日 16:07:41   投稿:lqh  
这篇文章主要介绍了java 中 request.getSession(true/false/null)的区别的相关资料,需要的朋友可以参考下

java 中 request.getSession(true/false/null)的区别

一、需求原因

现实中我们经常会遇到以下3中用法:

HttpSession session = request.getSession();

HttpSession session = request.getSession(true);

HttpSession session = request.getSession(false);

二、区别

1.      Servlet官方文档说:

public HttpSessiongetSession(boolean create)
Returns the currentHttpSession associated with this request or, if if there is no current sessionand create is true, returns a new session.
If create is falseand the request has no valid HttpSession, this method returns null.
To make sure thesession is properly maintained, you must call this method before the responseis committed. If the Container is using cookies to maintain session integrityand is asked to create a new session when the response is committed, anIllegalStateException is thrown.
Parameters: true -to create a new session for this request if necessary; false to return null ifthere's no current session
Returns: theHttpSession associated with this request or null if create is false and therequest has no valid session

2.      翻译过来的意思是:

getSession(boolean create)意思是返回当前reqeust中的HttpSession ,如果当前reqeust中的HttpSession 为null,当create为true,就创建一个新的Session,否则返回null;

简而言之:

HttpServletRequest.getSession(ture)等同于 HttpServletRequest.getSession() 
HttpServletRequest.getSession(false)等同于 如果当前Session没有就为null; 

3.      使用

当向Session中存取登录信息时,一般建议:HttpSession session =request.getSession();

当从Session中获取登录信息时,一般建议:HttpSession session =request.getSession(false);

4.      更简洁的方式

如果你的项目中使用到了Spring,对session的操作就方便多了。如果需要在Session中取值,可以用WebUtils工具(org.springframework.web.util.WebUtils)的WebUtils.getSessionAttribute(HttpServletRequestrequest, String name);方法,看看源码:

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

你使用时:

WebUtils.setSessionAttribute(request, "user", User);

User user = (User)WebUtils.getSessionAttribute(request, "user");

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

  • java反射使用示例分享

    java反射使用示例分享

    这篇文章主要介绍了java反射使用示例,代码很简单,需要的朋友可以参考下
    2014-02-02
  • 深入浅出学习AQS组件

    深入浅出学习AQS组件

    AQS ( AbstractQueuedSynchronizer)是一个用来构建锁和同步器的框架,使用AQS能简单且高效地构造出应用广泛的大量的同步器,下面小编和大家来一起学习一下吧
    2019-05-05
  • 一文详解Spring是怎么读取配置Xml文件的

    一文详解Spring是怎么读取配置Xml文件的

    这篇文章主要介绍了一文详解Spring是怎么读取配置Xml文件的,文章围绕主题展开详细的内容介绍,具有一定的参考价值,感兴趣的小伙伴可以参考一下
    2022-08-08
  • Java程序命令行参数用法总结

    Java程序命令行参数用法总结

    这篇文章主要介绍了Java程序命令行参数用法总结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • Spring MVC拦截器和跨域请求使用详解

    Spring MVC拦截器和跨域请求使用详解

    SpringMVC的拦截器也是AOP思想的一种实现方式,主要用于拦截用户的请求并做相应的处理,通常应用在权限验证、记录请求信息的日志、判断用户是否登录等功能上,这篇文章主要介绍了Spring MVC拦截器和跨域请求,需要的朋友可以参考下
    2023-07-07
  • Spring boot webService使用方法解析

    Spring boot webService使用方法解析

    这篇文章主要介绍了Spring boot webService使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • Java实现带有权重随机算法的示例详解

    Java实现带有权重随机算法的示例详解

    这篇文章主要为大家详细介绍了Java如何实现带有权重随机算法,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-10-10
  • Spring MVC全局异常实例详解

    Spring MVC全局异常实例详解

    这篇文章主要给大家介绍了关于Spring MVC全局异常的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-02-02
  • Java如何实现文件压缩与上传FTP

    Java如何实现文件压缩与上传FTP

    这篇文章主要介绍了Java如何实现文件压缩与上传FTP,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • 详解Spring mvc的web.xml配置说明

    详解Spring mvc的web.xml配置说明

    本篇文章主要介绍了Spring mvc的web.xml配置说明,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02

最新评论