How to provide a context configuration for a web application in Tomcat?

前端 未结 6 1539
星月不相逢
星月不相逢 2020-12-04 08:30

I have a web application that relies on some resources and parameters to be configured after it is installed, like a JDBC connection.

What I have come up with is pro

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-04 09:24

    This is how we can manage to externalize webapp context from .WAR File

    1. Place your .WAR file somewhere outside tomcat
    2. Create a $APP_NAME.xml file into $TOMCAT_HOME/conf/[Engine]/[Host]/ directory.
    3. Now file "$APP_NAME.xml" we just created need to have context definition and parameters + Any EnvironmentVariable you want specific to that context.

    For e.g. I have an webapp called VirtualWebApp.

    I will create file like VirtualWebApp.xml with below context definition :

    
        
        
    
    

    To access these environment variables you have to write below code(Just lookup) :

    InitialContext initialContext = new javax.naming.InitialContext();
    
    host = (String)initialContext.lookup("java:comp/env/webservice.host");
    port = (String)initialContext.lookup("java:comp/env/webservice.port");
    

提交回复
热议问题