How to modify tomcat web.xml location per webapplication

Tags: , , , ,



I’ve multiple spring boot applications deployed in my tomcat server (as war files). 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).

I searched and read tomcat docs. 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. But we don’t store app specific web.xml configurations in our source base.

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.

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. Tomcat will load these web.xml ignoring war’s default /WEB-INF/web.xml path.

Answer

I solved this problem by creating seperate context.xml for each webapp. According to tomcat docs context.xml has an attribute named altDDName which can override the absolute path of that webapp’s web.xml. According to tomcat docs:

altDDName
The absolute path to the alternative deployment descriptor for this context. This overrides the default deployment descriptor located at /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.xmls.

Example:

Suppose I’ve two apps A.war and B.war. My tomcat is hosted in localhost. I create two seperate context.xmls in $CATALINA_BASE/conf/Catalina/localhost/A.xml & $CATALINA_BASE/conf/Catalina/localhost/B.xml with the following property:

A.xml

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

and similarly 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:

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.

I hope there are better solution than this. But it solves my problem.



Source: stackoverflow