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

所属分类: 软件编程 / java 阅读数: 43
收藏 0 赞 0 分享

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");

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

更多精彩内容其他人还在看

浅谈spring注解之@profile

这篇文章主要介绍了浅谈spring注解之@profile,@profile通过配置来改变参数,这里整理的详细的用法,有兴趣的可以了解一下
收藏 0 赞 0 分享

Java this 关键字的使用方法详解

这篇文章主要介绍了Java this 关键字的使用方法详解的相关资料,希望通过本文能帮助到大家,让大家彻底理解掌握这部分内容,需要的朋友可以参考下
收藏 0 赞 0 分享

Java super关键字的使用方法详解

这篇文章主要介绍了Java super关键字的使用方法详解的相关资料,希望通过本文能帮助到大家,让大家对super关键字彻底掌握,需要的朋友可以参考下
收藏 0 赞 0 分享

springboot整合redis进行数据操作(推荐)

springboot整合redis比较简单,并且使用redistemplate可以让我们更加方便的对数据进行操作。下面通过本文给大家分享springboot整合redis进行数据操作的相关知识,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

java实现简单解析XML文件功能示例

这篇文章主要介绍了java实现简单解析XML文件功能,结合实例形式分析了java针对xml文件的读取、遍历节点及输出等相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Spring Java-based容器配置详解

这篇文章主要介绍了Spring Java-based容器配置详解,涉及注解和@Configuration类以及@Beans的相关知识,具有一定参考价值,需要的朋友可以了解。
收藏 0 赞 0 分享

java实现MD5加密的方法小结

这篇文章主要介绍了java实现MD5加密的方法,结合具体实例形式总结分析了java实现md5加密的常用操作技巧与使用方法,需要的朋友可以参考下
收藏 0 赞 0 分享

使用maven运行Java Main的三种方法解析

这篇文章主要介绍了使用maven运行Java Main的三种方式的相关内容,具有一定参考价值,需要的朋友可以了解下。
收藏 0 赞 0 分享

javaweb设计中filter粗粒度权限控制代码示例

这篇文章主要介绍了javaweb设计中filter粗粒度权限控制代码示例,小编觉得还是挺不错的,需要的朋友可以参考。
收藏 0 赞 0 分享

java 流操作对文件的分割和合并的实例详解

这篇文章主要介绍了java 流操作对文件的分割和合并的实例详解的相关资料,希望通过本文能帮助到大家,让大家理解掌握这部分内容,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多