当您的应用程序加载时,您可以从您的 init 参数中获取此信息并使用 ServletContextListener 保存以供其他时间使用,如下所示:
public class TestListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
String yourVariable = servletContextEvent.getServletContext().getInitParameter("yourParameter");
//Some code..
}
}
在您的 web.xml 中定义此侦听器:
<web-app ...>
<listener>
<listener-class>
com.fullanem.TestListener
</listener-class>
</listener>
</web-app>
在 web.xml 中定义您的参数:
<context-param>
<description>This is a context parameter example</description>
<param-name>yourParameter</param-name>
<param-value>Your value</param-value>
</context-param>
所以你把它存储在你想要的地方,作为属性,或者全局变量,或者任何地方。