[英]Get sessionCookieName from ServletContext in context.xml of tomcat
How could I get the name of session of current request? 如何获得当前请求的会话名称?
The default name of tomcat session is "jsessionid". Tomcat会话的默认名称是“ jsessionid”。 Given that I have modified the sessionCookieName configuration in context.xml of tomcat as below to gives multiple tomcat instance run on a same server
鉴于我已经修改了tomcat的context.xml中的sessionCookieName配置,如下所示,以使多个tomcat实例在同一服务器上运行
<Context sessionCookieName="appSessionId">
<!-- Default set of monitored resources. If one of these changes, the -->
<!-- web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
</Context>
My web app need to know what the name of session of current request is. 我的Web应用程序需要知道当前请求的会话名称是什么。 I have tried such as:
我尝试过例如:
request.getServletContext().getSessionCookieConfig().getName()
request.getSession().getServletContext().getSessionCookieConfig().getName()
But both of them are null. 但是他们两个都是空的。
My pom.xml as below 我的pom.xml如下
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>8.0.23</version>
</dependency>
The tomcat I have used is also 8.0.23, any help is appreciated. 我用过的tomcat也是8.0.23,任何帮助都是值得的。
This is pretty confusing, because from what I can tell the following always return null, so I'm not sure what the point in them is! 这非常令人困惑,因为从我可以看出的以下内容始终返回null,所以我不确定它们的意义是什么!
request.getServletContext().getSessionCookieConfig().getName()
request.getSession().getServletContext().getSessionCookieConfig().getName()
You need to do: 您需要做:
SessionConfig.getSessionCookieName(request.getContext())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.