简体   繁体   English

如何修改每个 web 应用程序的 tomcat web.xml 位置

[英]How to modify tomcat web.xml location per webapplication

I've multiple spring boot applications deployed in my tomcat server (as war files).我在我的 tomcat 服务器中部署了多个 Spring Boot 应用程序(作为战争文件)。 My problem is I want to customize tomcat's default session-timeout for some of the webapps (ie. without editing the global $CATALINA_BASE/conf/web.xml ).我的问题是我想为某些 web 应用程序自定义 tomcat 的默认session-timeout (即,不编辑全局$CATALINA_BASE/conf/web.xml )。

I searched and read tomcat docs.我搜索并阅读了 tomcat 文档。 It seems the only way I can achieve that is by creating /WEB-INF/web.xml inside my war files that needs different session-timeout value.似乎我可以实现的唯一方法是在需要不同session-timeout值的 war 文件中创建/WEB-INF/web.xml But we don't store app specific web.xml configurations in our source base.但是我们不会在我们的源代码库中存储特定于应用程序的web.xml配置。

So what I want to do is create a directory where I'll store all the webapp specific web.xml files and tell tomcat to load the web.xml file from there and not from my war's /WEB-INF/web.xml location.所以我想要做的是创建一个目录,我将在其中存储所有 webapp 特定的web.xml文件,并告诉 tomcat 从那里加载web.xml文件,而不是从我的战争的/WEB-INF/web.xml位置加载。

Example:例子:

For webapp A.war , B.war I want to have a directory $CATALINA_BASE/webxmls which has two web.xml inside like A_web.xml and B_web.xml .对于 webapp A.warB.war我想要一个目录$CATALINA_BASE/webxmls里面有两个 web.xml 像A_web.xmlB_web.xml Tomcat will load these web.xml ignoring war's default /WEB-INF/web.xml path. Tomcat 将加载这些 web.xml 忽略 war 的默认/WEB-INF/web.xml路径。

您可以通过以下方式更改 web.xml 的路径

<Context docBase="****" altDDName="$CATALINA_BASE/webxmls.xml">

I solved this problem by creating seperate context.xml for each webapp.我通过为每个 webapp 创建单独的context.xml解决了这个问题。 According to tomcat docs context.xml has an attribute named altDDName which can override the absolute path of that webapp's web.xml .根据 tomcat docs context.xml有一个名为altDDName的属性,它可以覆盖该 webapp 的web.xml的绝对路径。 According to tomcat docs :根据 tomcat文档

altDDName别名
The absolute path to the alternative deployment descriptor for this context.此上下文的备用部署描述符的绝对路径。 This overrides the default deployment descriptor located at /WEB-INF/web.xml.这将覆盖位于 /WEB-INF/web.xml 的默认部署描述符。

So setting the absolute path of my external web.xml in altDDName will allow me to externally store all my webapp specific web.xml s.所以在altDDName设置我的外部web.xml的绝对路径将允许我在外部存储我所有的 webapp 特定的web.xml s。

Example:例子:

Suppose I've two apps A.war and B.war .假设我有两个应用程序A.warB.war My tomcat is hosted in localhost.我的 tomcat 托管在 localhost 中。 I create two seperate context.xml s in $CATALINA_BASE/conf/Catalina/localhost/A.xml & $CATALINA_BASE/conf/Catalina/localhost/B.xml with the following property:我在$CATALINA_BASE/conf/Catalina/localhost/A.xml & $CATALINA_BASE/conf/Catalina/localhost/B.xml创建了两个单独的context.xml ,具有以下属性:

A.xml

<Context altDDName="$CATALINA_BASE/webxmls/A_web.xml">
</Context>

and similarly B.xml :和类似的B.xml

<Context altDDName="$CATALINA_BASE/webxmls/B_web.xml">
</Context>

Now in $CATALINA_BASE/webxmls/A_web.xml I can store my external web.xml where I can override webapp specific session-timeout :现在在$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>

Similarly create new B_web.xml for B.war .同样创造新B_web.xmlB.war

I hope there are better solution than this.我希望有比这更好的解决方案。 But it solves my problem.但它解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM