问题描述
我刚刚经历过,
直到现在我的印象是,
根据传递给该方法的 boolean
为我提供当前会话(给予,不创建).看起来很酷.
Gives me the current session(giving,not creating) based upon the boolean
passed to that method.Looks cool till here.
现在我读到了
Session 在您的代码第一次调用 request.getSession() 或 request.getSession(true) 时创建.
所以,如果我没有在我的任何 servlets
中调用 request.getSession()
,并且这些 servlets 是为了提供一些静态 html 页面(大约 50),
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),
1)容器和客户端之间不需要session
?
1)There is no need of a session
between the container and the client ?
2)如果没有容器如何检测(提供 html 页面)客户端?除了 session id
之外的标题中的任何隐藏信息?
2)If no how container is detecting(serving a html page) client ?any hidden info in headers other than the session id
?
推荐答案
HttpSession
并非总是必需的.如果 servlet 是无状态的",并且来自 HTTP 请求的信息足以满足请求,那么就是这种情况.
A HttpSession
is 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.
因此,如果您有不调用 request.getSession()
的 servlet,则不会创建 HttpSession
.
So a HttpSession
is not created, if you have servlets which do not call request.getSession()
.
一般来说 HttpSession
是必需的,如果 servlet 必须检测多个请求是否来自同一个客户端.例如,在会话属性中管理会话状态(如购物车等).
Generally speaking the HttpSession
is 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.
示例:telnet
进入仅返回文本/纯字符串的 servlet:粗体 中的文本已输入(即 HTTP 请求)
Example: telnet
into a servlet which only returns a text/plain string: The text in bold has been typed in (that's the HTTP request)
$ telnet 本地主机 8080
正在尝试 127.0.0.1...
连接到 localhost.localdomain.
转义字符是^]".
GET/xxx/textplainservlet/HTTP/1.1
主机:本地主机:8080
HTTP/1.1 200 正常
服务器:Apache-Coyote/1.1
内容类型:文本/纯文本;字符集=ISO-8859-1
内容长度:13
日期:2013 年 9 月 6 日星期五 12:11:10 GMT
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
你好,世界
在这种情况下不会创建会话.
A sesion is not created in this case.
示例:一个简单的 JSP,它只返回一个静态 HTML 内容:
Example: A simple JSP which returns nothing but a static HTML content:
GET/xxx/hello.jsp HTTP/1.1
主机:本地主机:8080
HTTP/1.1 200 正常
服务器:Apache-Coyote/1.1
X-Powered-By:JSP/2.2
设置-Cookie:JSESSIONID=n0cOaZFUvlXSvX7hNEfcNzHP.undefined;路径=/nk-eapp-ping-60-jpa
内容类型:文本/html;字符集=ISO-8859-1
内容长度:49
日期:2013 年 9 月 6 日星期五 12:11:58 GMT
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
[...一个 HTML 文档...]
[... a HTML document ...]
在这种情况下,会创建一个会话,并设置 cookie,即使 JSP 没有调用 request.getSession()
明确!
In that case, a session is created, and the cookie is set, even if the JSP does not call request.getSession()
explicitly!
因此,我附加了一个 HttpSessionListener
,实际上,会话是隐式创建的.在那个监听器中,我转储了一个堆栈跟踪:
Therefore I have attached a HttpSessionListener
, and indeed, a session is created implicitly. In that listener I dumped a stack trace:
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)
这些测试是使用 JBoss 7 运行的.
These tests have been run using JBoss 7.
要检查是否创建了会话,只需使用 HttpSessionListener
在您的环境中重新测试它:
To check if a session is created or not, just re-test it in your environment using a 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());
}
}
这篇关于request.getSession() 是获取会话还是创建会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!