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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30980616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:27:41  来源:igfitidea点击:

Difference between request.getSession() and request.getSession(true)

javajsphttpsessionservlets

提问by Ali Khan

I understand the difference between request.getSession(true)and request.getSession(false). But request.getSession()& request.getSession(true)look very similar!

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

Both "return the current session associated with this request", but differ in:

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

request.getSession():

request.getSession()

"or if the request does not have a session, creates one"

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

request.getSession(true):

request.getSession(true)

"if there is no current session, returns a new session"

“如果没有当前会话,则返回一个新会话

I don't understand the difference between them, is it that (if none exists) they create a new session but the first one doesn't returnit but the second one does?

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

Source: http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html

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

Edit:

编辑:

Someone tagged/marked my question as duplicate even though it isn't. I will explain why.

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

I have explicitly asked for the difference between request.getSession()& request.getSession(true)and NOT between request.getSession(true)& request.getSession(false)! I have stated , again explicitly, that I already understand the difference b/w ..(true)& ..(false).

我已经明确要求request.getSession()&request.getSession(true)和 NOT 之间的区别request.getSession(true)& request.getSession(false)!我已经再次明确表示,我已经理解了 b/w ..(true)&的区别..(false)

The question linkedas a possible duplicated of of asks about the difference b/w ..(true)& ..(false)and not ..(true)& ..()

问题链接作为一个可能重复的询问差值B / W ..(true)..(false)而不是..(true)..()

采纳答案by Jan

request.getSession()is just a convenience method. It does exactly the same as request.getSession(true).

request.getSession()只是一种方便的方法。它的作用与request.getSession(true).

回答by mike

They both return the same thing, as noted in the documentation you linked; an HttpSession object.

正如您链接的文档中所述,它们都返回相同的内容;一个 HttpSession 对象。

You can also look at a concrete implementation (e.g. Tomcat) and see what it's actually doing: Request.javaclass. In this case, basically they both call:

您还可以查看具体实现(例如Tomcat)并查看它实际在做什么:Request.java类。在这种情况下,基本上他们都调用:

Session session = doGetSession(true);

回答by Ranjitsinh

request.getSession()will return a current session. if current session does not exist, then it will create a new one.

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

request.getSession(true)will return current session. If current session does not exist, then it will create a new session.

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

So basically there is not difference between both method.

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

request.getSession(false)will return current session if current session exists, then it will notcreate a new session.

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

回答by Ruby Kannan

Method with boolean argument :

带有布尔参数的方法:

  request.getSession(true);

returns new session, if the session is not associated with the request

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

  request.getSession(false);

returns null, if the session is not associated with the request.

如果会话与请求无关,则返回 null。

Method without boolean argument :

没有布尔参数的方法:

  request.getSession();

returns new session, if the session is not associated with the request and returns the existing session, if the session is associated with the request.It won't return null.

返回新会话,如果会话与请求无关,返回现有会话,如果会话与请求相关联,则不会返回空值。

回答by NPE

request.getSession() or request.getSession(true) both will return a current session only . if current session will not exist then it will create a new session.

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

回答by stephan obwins

request.getSession(true)and request.getSession()both do the same thing, but if we use request.getSession(false)it will return nullif session object not created yet.

request.getSession(true)并且request.getSession()都做同样的事情,但如果我们用 request.getSession(false)它将返回null如果会话对象尚未创建。

回答by stephan obwins

A major practical difference is its use:

一个主要的实际区别是它的用途:

in security scenariowhere we always needed a new session, we should use request.getSession(true).

security scenario我们总是需要一个新会话的地方,我们应该使用request.getSession(true).

request.getSession(false): will return null if no session found.