简体   繁体   English

Java Web应用程序启动时

[英]Java Web Application on startup

Is there a way to execute java code, when a Java EE (Java web project) starts up (or receives a first request)? 当Java EE(Java Web项目)启动(或接收到第一个请求)时,是否可以执行Java代码? I know that servlets have init() methods, but is there such method for an entire application? 我知道servlet具有init()方法,但是整个应用程序是否都具有这种方法?

Have a class that 上一堂课

implements ServletContextListener

and in your web.xml 并在您的web.xml中

<listener>
    <listener-class> myPackage.MyServletContextListener </listener-class>
</listener>

see http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContextListener.html 参见http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContextListener.html

and override 并覆盖

void contextInitialized(ServletContextEvent sce)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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