request.getSession(true)和request.getSession(false)的区别

request.getSession(true):若存在会话则返回该会话,否则新建一个会话。

request.getSession(false):若存在会话则返回该会话,否则返回NULL。

三种重载方法

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

HttpSession session = request.getSession();
HttpSession session = request.getSession(true);
HttpSession session = request.getSession(false);

三种重载方法的区别

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 if there‘s no current session.
Returns: theHttpSession associated with this request or null if create is false and therequest has no valid session.

翻译过来的意思就是:getSession(boolean create)是返回当前reqeust中的HttpSession ,如果当前reqeust中的HttpSession为null,当create为true,就创建一个新的Session;当create为false,则返回null。create的默认值为true,即这里的HttpServletRequest.getSession(ture)等同于HttpServletRequest.getSession()。

根据这个特性,在使用上,当向Session中存取登录信息时,一般建议使用request.getSession()/request.getSession(true);当从Session中只获取登录信息时,一般建议使用request.getSession(false)。

当然了,现在各种Web框架都会封装一些方便存取Session的工具类,一般是不建议直接从request中获取Session或前台参数什么的。

"不要轻易把伤口揭开给别人看,因为别人看的是热闹,而痛的却是自己。"

原文地址:https://www.cnblogs.com/yanggb/p/11156965.html

时间: 2024-08-04 09:49:23

request.getSession(true)和request.getSession(false)的区别的相关文章

转:request.getSession(true)和request.getSession(false)的区别

转自:http://www.cnblogs.com/tv151579/p/3870905.html 1.转自:http://wenda.so.com/q/1366414933061950?src=150 概括: 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)的区别 一.需求原因 现实中我们经常会遇到以下3中用法: HttpSession session = request.getSession(); HttpSession session = request.getSession(true); HttpSession session = request.getSessi

javaweb中重定向和请求转发(response.sendRedirect()和request.getRequestDispatcher(rul).forward(request,response)))的区别

先来两张图,方便理解: 可以看出,重定向时,是服务器向游览器重新发送了一个response命令,让游览器再次向url2发送请求,以获取url2的资源 而请求转发时,类似于是服务器自己向自己发了一个跳转,然后将结果直接给游览器,这也是问什么游览器会不改变url地址.下面是具体总结他们的区别 一.response.sendRedirect(url)-服务器将新url发送给游览器,游览器再根据新url请求 Request.getRequestDispatcher(url).forward(reques

jquery 中cache为true与false 的区别

$.ajax({ type: "get", cache: false, url: url, success: function (msg) { } }); cache为true与false 的区别 true的话会读缓存,可能真的到服务器上. 假如上次访问了a.html,第二次的时候得到的是上次访问的a.html的结果,而不是重新到服务器获取. false的话会在url后面加一个时间缀,让它跑到服务器获取结果. cache只有GET方式的时候有效

今天面试问了一道题。说一串字符串由这几个符号组成"<>{}[]()”,写一个算法,例如如果组成方式为“<>{[]}{}()”这种,也就是XML格式那种则返回true。否则返回false;

原创 今天面试问了一道题.说一串字符串由这几个符号组成"<>{}[]()",写一个算法,例如如果组成方式为"<>{[]}{}()"这种,也就是XML格式那种则返回true.否则返回false: 当时没想出来, 只想到了回文数解决办法.回文数解决办法是颠倒比较,相等为true: 换xml格式当时真没想到啥好方法: 在回来的路上想到了.. .. 去重,吧临近的一组去掉,在递归调用比较去重直到最后,如果有剩下则不返回false:否则true: 代码

Python 字典(Dictionary) has_key()方法-判断键是否存在于字典中,如果键在字典dict里返回true,否则返回false

描述 Python 字典(Dictionary) has_key() 函数用于判断键是否存在于字典中,如果键在字典dict里返回true,否则返回false. 语法 has_key()方法语法: dict.has_key(key) 参数 key -- 要在字典中查找的键. 返回值 如果键在字典里返回true,否则返回false. 实例 以下实例展示了 has_key()函数的使用方法: #!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7} pri

request.getContextPath();与${pageContext.request.contextPath}

(1) request.getContextPath();与${pageContext.request.contextPath}都是获取上下文路径: 1. request.getContextPath();在普通的java代码中用,当然也可以在jsp中用:<% String contextPath = request.getContextPath(); %> 2. ${pageContext.request.contextPath} jsp中el表达式,在jsp页面中用: (2) 不同的部署w

python 中的True和1及False和0是可以等价比较

python 中的True和1及False和0是可以等价比较的测试如下: [[email protected] root]# cat test_true_false.py ok=0if ok:    print okok=1 if ok:    print okok=True if ok:    print okok=False if ok:    print ok print '*'*20 ok=0if ok == False:    print ok ok=1if ok == True:  

response.sendRedirect(url)与request.getRequestDispatcher(url).forward(request,response)的区别

response.sendRedirect(url)跳转到指定的URL地址,产生一个新的request,所以要传递参数只有在url后加参数,如: url?id=1.request.getRequestDispatcher(url).forward(request,response)是直接将请求转发到指定URL,所以该请求能够直接获得上一个请求的数据,也就是说采用请求转发,request对象始终存在,不会重新创建.而sendRedirect()会新建request对象,所以上一个request中的