Tomcat adds context configuration, which causes the project to be loaded twice during startup

Time:2020-11-5

Tomcat demining notes

1. Scene
Eclipse publishes a web application to Tomcat. By default, accessing the project requires the project name http://localhost :8080/myapp/。
Now it needs to be changed to this way http://localhost 。
Modify Tomcat’s server.xml File, add the following configuration<Context path="" docBase="myapp" reloadable="false"/>After modification, the host part is as follows:

<Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

       <Context path="" docBase="myapp" reloadable="false"/>

      </Host>

At this point, every time you start Tomcat to observe the log, you will find that the application has been loaded twice. Why?

2. Cause of the problem

For the problem caused by virtual directory, we configured appbase = webapps in the host tag, and Tomcat loaded the application once. Docbase is configured once in, and Tomcat loads the application again.

3. Solutions

Set appbase = webapps to appbase = and docbase = myapp to docbase = webapps / myapp. The configuration is as follows:

<Host name="localhost"  appBase=""
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

       <Context path="" docBase="webapps/myapp" reloadable="false"/>

      </Host>

Note: if the links or pictures in your project are all absolute paths, then the absolute path with the project name cannot be used.

Recommended Today

Regular expression sharing for checking primes

This regular expression is shown as follows: Regular expressions for checking prime numbers or not To use this positive regular expression, you need to convert the natural number into multiple 1 strings. For example, 2 should be written as “11”, 3 should be written as “111”, 17 should be written as “11111111111”. This kind of […]