Java request.getSession() 是获取会话还是创建会话?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18651278/
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-12 09:51:06  来源:igfitidea点击:

request.getSession() is getting session or creating session?

javasessionservletscreation

提问by Suresh Atta

Just I gone through,

刚刚我走过,

Under what conditions is a JSESSIONID created?

JSESSIONID 是在什么条件下创建的?

Till now I am in a impression that,

到现在我的印象是,

request.getSession()

request.getSession()

Gives me the current session(giving,not creating) based upon the booleanpassed to that method.Looks cool till here.

根据传递给该方法的内容为我提供当前会话(给予,不创建boolean。直到这里看起来很酷。

Now I read that

现在我读到了

Session is created when your code calls request.getSession() or request.getSession(true) for the first time.

当您的代码第一次调用 request.getSession() 或 request.getSession(true) 时会创建会话。

So ,If I'm not calling request.getSession()in my any of servlets, And those servlets are made to serve some static html pages (around 50),

所以,如果我没有调用 request.getSession()我的任何一个servlets,并且这些 servlet 被用来提供一些静态 html 页面(大约 50 个),

1)There is no need of a sessionbetween the container and the client ?

1)session容器和客户端之间不需要a ?

2)If no how container is detecting(serving a html page) client ?any hidden info in headers other than the session id?

2)如果没有容器如何检测(提供 html 页面)客户端session id

回答by Nandkumar Tekale

1)There is no need of a session between the container and the client ?

1)容器和客户端之间不需要会话?

---> Not necessary if it is just html pages, for e.g. JavaDocs html pages, you do not need session to create.

---> 如果只是 html 页面,则不需要,例如 JavaDocs html 页面,您不需要 session 来创建。

2)If no how container is detecting(serving a html page) client ?any hidden info in headers other than the session id?

2)如果没有容器如何检测(提供 html 页面)客户端?除会话 ID 之外的标题中是否有任何隐藏信息?

----> It is the URL and you map url with html page or you just keep your pages with public access. Here, if request is hit, tomcat will create thread, which will serve the request by writing the request page in response.

----> 它是 URL,您将 url 与 html 页面映射,或者您只是让您的页面具有公共访问权限。在这里,如果请求被命中,tomcat 将创建线程,该线程将通过编写请求页面作为响应来为请求提供服务。

Take a look at HTTP Request header

看一下HTTP 请求头

回答by Beryllium

A HttpSessionis not always required. This is the case, if the servlet is "stateless", and the information from the HTTP request is sufficient to fulfill the request.

AHttpSession并不总是必需的。如果 servlet 是“无状态的”,并且来自 HTTP 请求的信息足以满足请求,那么就是这种情况。

So a HttpSessionis not created, if you have servlets which do not call request.getSession().

所以 aHttpSession不会被创建,如果你有不调用request.getSession().

Generally speaking the HttpSessionis required, if the servlet has to detect if multiple requests come from the same client. For example to manage conversational state (like a shopping cart etc.) in a session attribute.

一般来说,这HttpSession是必需的,如果 servlet 必须检测多个请求是否来自同一个客户端。例如,在会话属性中管理会话状态(如购物车等)。



Example: telnetinto a servlet which only returns a text/plain string: The text in boldhas been typed in (that's the HTTP request)

示例:telnet进入仅返回文本/纯字符串的 servlet:已输入粗体文本(即 HTTP 请求)

$ telnet localhost 8080
Trying 127.0.0.1...
Connected to localhost.localdomain.
Escape character is '^]'.
GET /xxx/textplainservlet/ HTTP/1.1
Host: localhost:8080

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/plain;charset=ISO-8859-1
Content-Length: 13
Date: Fri, 06 Sep 2013 12:11:10 GMT

Hello, world

$ telnet localhost 8080
正在尝试 127.0.0.1 ...
连接到 localhost.localdomain。
转义字符是“^]”。
GET /xxx/textplainservlet/ HTTP/1.1
主机:本地主机:8080

HTTP/1.1 200 OK
服务器:Apache-Coyote/1.1
内容类型:text/plain;charset=ISO-8859-1
内容长度:13
日期:2013 年 9 月 6 日星期五 12:11:10 GMT

你好,世界

A sesion is not created in this case.

在这种情况下不会创建会话。



Example: A simple JSP which returns nothing but a static HTML content:

示例:一个简单的 JSP,它只返回一个静态 HTML 内容:

GET /xxx/hello.jsp HTTP/1.1
Host: localhost:8080

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Powered-By: JSP/2.2
Set-Cookie: JSESSIONID=n0cOaZFUvlXSvX7hNEfcNzHP.undefined; Path=/nk-eapp-ping-60-jpa
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 49
Date: Fri, 06 Sep 2013 12:11:58 GMT

[... a HTML document ...]

GET /xxx/hello.jsp HTTP/1.1
主机:本地主机:8080

HTTP/1.1 200 OK
服务器:Apache-Coyote/1.1
X-Powered-By:JSP/2.2
Set-Cookie:JSESSIONID=n0cOaZFUvlXSvX7hNEfcNzHP.undefined;路径=/nk-eapp-ping-60-jpa
内容类型:text/html;charset=ISO-8859-1
内容长度:49
日期:2013 年 9 月 6 日星期五 12:11:58 GMT

[...一个 HTML 文档...]

In that case, a session is created, and the cookieis set, even if the JSP does not call request.getSession()explicitly!

在这种情况下,会创建一个会话,并设置cookie,即使 JSP 没有request.getSession()显式调用!

Therefore I have attached a HttpSessionListener, and indeed, a session is created implicitly. In that listener I dumped a stack trace:

因此,我附加了一个HttpSessionListener,实际上,隐式创建了一个会话。在那个监听器中,我转储了一个堆栈跟踪:

org.apache.catalina.session.StandardSession.tellNew(StandardSession.java:374)
org.apache.catalina.session.StandardSession.setId(StandardSession.java:344)
org.apache.catalina.session.ManagerBase.createSession(ManagerBase.java:506)
org.apache.catalina.session.StandardManager.createSession(StandardManager.java:297)
org.apache.catalina.connector.Request.doGetSession(Request.java:2665)
org.apache.catalina.connector.Request.getSession(Request.java:2375)
org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:841)
org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:852)
org.apache.jasper.runtime.PageContextImpl._initialize(PageContextImpl.java:146)
org.apache.jasper.runtime.PageContextImpl.initialize(PageContextImpl.java:124)
org.apache.jasper.runtime.JspFactoryImpl.internalGetPageContext(JspFactoryImpl.java:106)
org.apache.jasper.runtime.JspFactoryImpl.getPageContext(JspFactoryImpl.java:62)
org.apache.jsp.hello_jsp._jspService(hello_jsp.java:45)

org.apache.catalina.session.StandardSession.tellNew(StandardSession.java:374)
org.apache.catalina.session.StandardSession.setId(StandardSession.java:344)
org.apache.catalina.session.ManagerBase.createSession(ManagerBase. java:506)
org.apache.catalina.session.StandardManager.createSession(StandardManager.java:297)
org.apache.catalina.connector.Request.doGetSession(Request.java:2665)
org.apache.catalina.connector.Request。 getSession(Request.java:2375)
org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:841)
org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:852)
org.apache.jasper。 runtime.PageContextImpl._initialize(PageContextImpl.java:146)
org.apache.jasper.runtime.PageContextImpl.initialize(PageContextImpl.java:124)
org.apache.jasper.runtime.JspFactoryImpl.internalGetPageContext(JspFactoryImpl.java:106)
org.apache.jasper.runtime.JspFactoryImpl.getPageContext(JspFactoryImpl. java:62)
org.apache.jsp.hello_jsp._jspService(hello_jsp.java:45)

These tests have been run using JBoss 7.

这些测试是使用 JBoss 7 运行的。



To check if a session is created or not, just re-test it in your environment using a HttpSessionListener:

要检查会话是否已创建,只需使用以下命令在您的环境中重新测试它HttpSessionListener

@WebListener
public class MyHttpSessionListener implements HttpSessionListener {
    private final static Logger log = Logger
            .getLogger(MyHttpSessionListener.class.getName());

    public void sessionCreated(HttpSessionEvent e) {
        // Possibly create a stack trace here, and dump it
        log.info("Session created: " + e.getSession().getId() + ", timeout "
                + e.getSession().getMaxInactiveInterval());
    }

    public void sessionDestroyed(HttpSessionEvent e) {
        log.info("Session destroyed: " + e.getSession().getId());
    }
}