简体   繁体   中英

How to access context.xml parameter?

I am developing an application and deploying it in Tomcat 7. I have defined some parameters in /etc/tomcat7/context.xml. How can I access them from my Java application?

context.xml

<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <Parameter name="my-param" value="my-string-value"/>
</Context>

I created ServletContextListener and got all I needed. Thank you for your help.

public class BackgroundJobManager implements ServletContextListener {

    /**
     * Reads params from context.xml
     * @param event
     */
    @Override
    public void contextInitialized(ServletContextEvent event) {
            String param = event.getServletContext().getInitParameter("my-param");
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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