![](/img/trans.png)
[英]How to get resource from the context.xml file in tomcat webapp?
[英]Get sessionCookieName from ServletContext in context.xml of tomcat
如何獲得當前請求的會話名稱?
Tomcat會話的默認名稱是“ jsessionid”。 鑒於我已經修改了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>
我的Web應用程序需要知道當前請求的會話名稱是什么。 我嘗試過例如:
request.getServletContext().getSessionCookieConfig().getName()
request.getSession().getServletContext().getSessionCookieConfig().getName()
但是他們兩個都是空的。
我的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>
我用過的tomcat也是8.0.23,任何幫助都是值得的。
這非常令人困惑,因為從我可以看出的以下內容始終返回null,所以我不確定它們的意義是什么!
request.getServletContext().getSessionCookieConfig().getName()
request.getSession().getServletContext().getSessionCookieConfig().getName()
您需要做:
SessionConfig.getSessionCookieName(request.getContext())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.