request.getSession()和request.getSession(true)之间的区别

Ali*_*han 36 java session jsp servlets http

我明白之间的差别request.getSession(true)request.getSession(false).但是,request.getSession()request.getSession(true)看起来非常相似!

两者都"返回与此请求关联的当前会话",但不同之处在于:

request.getSession():

"或者如果请求没有会话,则创建一个会话"

request.getSession(true):

"如果没有当前会话,则返回新会话 "

我不明白它们之间的区别,是否(如果不存在)它们创建一个新会话但​​第一个不返回它但第二个会返回它?

资料来源:http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html

编辑:

有人将我的问题标记/标记为重复,即使它不是.我会解释原因.

我已经明确要求request.getSession()&request.getSession(true)之间的区别request.getSession(true)&request.getSession(false)!我再次明确地说过,我已经理解了b/w ..(true)&的不同之处..(false).

这个问题被链接为可能重复的关于差异的问题b/w ..(true)&..(false)而不是..(true)&..()

Ran*_*inh 65

request.getSession()将返回当前会话.如果当前会话不存在,那么它将创建一个新会话.

request.getSession(true)将返回当前会话.如果当前会话不存在,那么它将创建一个新会话.

所以两种方法基本没有区别.

request.getSession(false)如果当前会话存在,将返回当前会话,然后它将不会创建新会话.


Jan*_*Jan 36

request.getSession()只是一种方便的方法.它完全一样request.getSession(true).


小智 5

布尔参数的方法:

  request.getSession(true);
Run Code Online (Sandbox Code Playgroud)

如果会话未与请求关联,则返回新会话

  request.getSession(false);
Run Code Online (Sandbox Code Playgroud)

如果会话未与请求关联,则返回null.

没有布尔参数的方法:

  request.getSession();
Run Code Online (Sandbox Code Playgroud)

如果会话与请求无关,则返回新会话,如果会话与请求关联,则返回现有会话.它不会返回null.