Load the context / servlet on startup in Tomcat * WITHOUT * deployment descriptor changes (web.xml)

I have a file foo.warfrom a third-party provider. I defined the context in my Tomcat configuration by creating conf/Catalina/localhost/foo.xmlone that contains:

<Context docBase="/path/to/foo.war" ...> ... </Context>

I want Tomcat to load the foo context at startup. But WEB-INF/web.xml(deployment descriptor) foo.wardoes not contain <load-on-startup> in the file , so Tomcat waits until the first request. I would prefer not to unzip the third-party foo.warto edit their web.xml. In addition, I will have to do this every time the provider releases a new version of their .war.

Is there any way in the Tomcat configuration to tell Tomcat to load the foo context at startup? I know that in an element <Context>you can set parameters, env vars, etc. Without editing web.xml. But I can not find anything in Tomcat's docs about loading at startup.

+3
source share
1 answer

It's complicated. You are limited by the conventions of Tomcat and other containers, so there is no easy solution.

web.xml / JSP .war <load-on-startup>. , .war WEB-INF/web.xml . , JSP /, .

, , .war , web.xml, , . , .war . script, .war web.xml web.xml.

, script .war, script, WEB-INF/web.xml .war, <load-on-startup> <servlet> .war . , Tomcat, bash script, :

#!/bin/sh

TEMPDIR=/tmp/temp$$
WARFILE=/path-to-tomcat/webapps/foo.war

mkdir -p $TEMPDIR/WEB-INF
pushd $TEMPDIR
unzip -qq -c $WARFILE WEB-INF/web.xml \
    | sed 's#</servlet>.*#<load-on-startup>99</load-on-startup></servlet>#' \
    > WEB-INF/web.xml
zip -f $WARFILE WEB-INF/web.xml
popd
rm -rf $TEMPDIR

script - , Tomcat. , .

+3

Source: https://habr.com/ru/post/1702165/


All Articles