我在 tomcat 服务器中部署了多个 spring boot 应用程序(作为 war 文件)。我的问题是我想自定义tomcat的默认session-timeout
对于某些网络应用程序(即无需编辑全局 $CATALINA_BASE/conf/web.xml
)。
我搜索并阅读了 tomcat 文档。我实现这一目标的唯一方法似乎是创建 /WEB-INF/web.xml
在我的 war 文件中需要不同的 session-timeout
值(value)。但我们不存储特定于应用程序的 web.xml
我们的源库中的配置。
所以我想做的是创建一个目录,在其中存储所有特定于 webapp 的 web.xml
文件并告诉 tomcat 加载 web.xml
来自那里的文件而不是来 self 的 war 的 /WEB-INF/web.xml
位置。
示例:
对于网络应用程序 A.war
, B.war
我想要一个目录$CATALINA_BASE/webxmls
其中有两个 web.xml ,如 A_web.xml
和B_web.xml
。 Tomcat 将加载这些 web.xml,忽略 war 的默认 /WEB-INF/web.xml
路径。
最佳答案
我通过为每个 Web 应用程序创建单独的 context.xml
解决了这个问题。根据 tomcat 文档 context.xml
有一个名为 altDDName
的属性,它可以覆盖该 webapp 的 web.xml
的绝对路径。根据tomcatdocs :
altDDName
The absolute path to the alternative deployment descriptor for this context. This overrides the default deployment descriptor located at /WEB-INF/web.xml.
因此,在 altDDName
中设置外部 web.xml
的绝对路径将允许我在外部存储所有特定于 Web 应用程序的 web.xml
s。
示例:
假设我有两个应用程序 A.war
和 B.war
。我的 tomcat 托管在 localhost 中。我在 $CATALINA_BASE/conf/Catalina/localhost/A.xml
和 $CATALINA_BASE/conf/Catalina/localhost/B 中创建了两个单独的
具有以下属性:context.xml
。 xml
A.xml
<Context altDDName="$CATALINA_BASE/webxmls/A_web.xml">
</Context>
类似的B.xml
:
<Context altDDName="$CATALINA_BASE/webxmls/B_web.xml">
</Context>
现在在 $CATALINA_BASE/webxmls/A_web.xml
中,我可以存储我的外部 web.xml
,我可以在其中覆盖 webapp 特定的 session-timeout
>:
A_web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>
同样为B.war
创建新的B_web.xml
。
我希望有比这更好的解决方案。但它解决了我的问题。
关于java - 如何修改每个 web 应用程序的 tomcat web.xml 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63781257/