转:request.getSession(true)和request.getSession(false)的区别
1.转自:http://wenda.so.com/q/1366414933061950?src=150
概括:
request.getSession(true):若存在会话则返回该会话,否则新建一个会话。
request.getSession(false):若存在会话则返回该会话,否则返回NULL
===========================================================================
2.转自:http://blog.csdn.net/gaolinwu/article/details/7285783
一、需求原因
现实中我们经常会遇到以下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)的getSessionAttribute(HttpServletRequestrequest, String name)方法,看看源码:
publicstatic 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”);
源码:

/**
* Set the session attribute with the given name to the given value.
* Removes the session attribute if value is null, if a session existed at all.
* Does not create a new session if not necessary!
* @param request current HTTP request
* @param name the name of the session attribute
*/
public static void setSessionAttribute(HttpServletRequest request, String name, Object value) {
if (value != null) {
request.getSession().setAttribute(name, value);
} else {
HttpSession session = request.getSession(false);
if (session != null) {
session.removeAttribute(name);
}
}
}

三、运行结果
以上例子均测试验证通过。
转:request.getSession(true)和request.getSession(false)的区别的更多相关文章
- request.getSession(true)和request.getSession(false)的区别
request.getSession(true)和request.getSession(false)的区别 request.getSession(true):若存在会话则返回该会话,否则新建一个会 ...
- 关于request.getsession(true|false)
request.getSession(true):若存在会话则返回该会话,否则新建一个会话.request.getSession(false):若存在会话则返回该会话,否则返回NULL
- 【转】于request.getSession(true/false/null)的区别
http://blog.csdn.net/gaolinwu/article/details/7285783 关于request.getSession(true/false/null)的区别 一.需求原 ...
- request.getSession(true/false)的区别
javax.servlet.http.HttpServletRequest接口有两个方法:getSession(boolean)和getSession(). 具体什么区别,跟踪源码分析下,先摆出结论: ...
- request.getSession()、reqeust.getSession(false)和request.getSession(true)
getSession()/getSession(true):当session存在时返回该session,否则新建一个session并返回该对象 getSession(false):当session存在 ...
- 如何获得 request, "request.getSession(true).setAttribute("a",a);"与“request.setAttribute("a",a);”区别
protected ServletContext getServletContext() { return ServletActionContext.getServletContext();} pro ...
- String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";作用!!!!!
<%String path = request.getContextPath();String basePath = request.getScheme()+"://"+re ...
- ***php解析JSON二维数组字符串(json_decode函数第二个参数True和False的区别)
客户端的请求体中的数据:[{"msg_id": 1, "msg_status": "HAS_READ" }, { "msg_id& ...
- request.getAttribute( "result");和request.setAttribute("result",username);
request.setAttribute("result",username);在request对象中加入名为result的属性并附值为username,因为request对象是可 ...
随机推荐
- iOS打包后收不到推送信息
今天遇到的一个特别神奇的问题: 应用在测试环境下打的包收不到推送了,打包之后都没有推送,但是我真机测试又是可以收到推送消息的.经过好久才找到原因,感觉很坑.这里记录一下问题: 1.由于我的推送第三方使 ...
- Ubuntu 16.04 设置MySQL远程访问权限
本文记录一下在Ubuntu 16.04版本下设置MySQL数据库的远程访问. 第一步:修改配置文件的端口绑定 打开的目录可能会根据MySQL的版本稍有不同,可以先尝试打开/etc/mysql/my.c ...
- 【C语言】printf()函数详解
printf函数称为格式输出函数,其关键字最末一个字母f即为"格式"(format)之意.其功能是按用户指定的格式,把指定的数据显示到显示器屏幕上.在前面的例题中我们已多次使用过这 ...
- MySQL---事务知识,你搞明白没有?
MySQL - 事务 在学习事务这一概念前,我们需要需要构思一个场景 场景构思 假设该场景发生于一个银行转账背景下,月中,又到了发工资的日子.潭州教育科技集团打算给Tuple老师发放一个月的工资.(此 ...
- SQL Server 给表和字段添加说明
.添加表说明 EXECUTE sp_addextendedproperty N'MS_Description','表说明',N'user',N'dbo',N'table',N'表名',NULL,NUL ...
- 老李分享:loadrunner的java user脚本开发
老李分享:loadrunner的java user脚本开发 poptest在性能测试loadrunner的课程里,以web协议为主,同时也讲解其他协议的脚本开发,对于一个性能测试工程师需要掌握一个以上 ...
- memcached预热失败
缓存逻辑:set和get方法通过中间层SocketServer,由SocketServer去操作缓存(同步备用缓存等) 预热逻辑:在SocketServer里面打日志,通过日志去预热新缓存 出现问题: ...
- css常用技巧集合
1 不想让按钮touch时有蓝色的边框或半透明灰色遮罩(根据系统而定) /*解决方式一*/ -webkit-tap-highlight-color:rgba(0,0,0,0); -webkit-use ...
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(九)数据层优化-jdbc连接池简述、druid简介
日常啰嗦 终于回到既定轨道上了,这一篇讲讲数据库连接池的相关知识,线程池以后有机会再结合项目单独写篇文章(自己给自己挖坑,不知道什么时候能填上),从这一篇文章开始到本阶段结束的文章都会围绕数据库和da ...
- sql server 数值的四舍五入
sql中的四舍五入通常会有round 和cast( …… as decimal())两种方式: 个人建议使用cast 方式: 方式1: 经过试验,同样都可以做到四舍五入,但round如下实例1会报 ...