在J2EE中,只要在服务器上发生某些操作(上下文创建,销毁,请求或会话属性添加,删除等),Listener就会通知。
请在下面找到下面的示例代码听众:
ApplicationListener类(在你的项目): -
package com.myproject;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ApplicationListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println(" Server Starting !!!!!! ");
//Any other code you can place here
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println(" Server Shutting down !!!!!! ");
}
}
的web.xml的变化: 添加下面的代码到您的网页。 xml
<listener>
<listener-class>
com.myproject.ApplicationListener
</listener-class>
</listener>
此外,请确保临时你的类路径中有“servlet-api.jar”文件。
我想在服务器启动完成后收到通知。上述contextInitialized将在启动期间调用以启动我们的上下文类。我想在完成服务器启动后发出通知。那是可能的 – user2641906
我可以知道,在服务器启动后,您想要处理的确切操作是什么? – developer
是的..服务器启动后,我需要通过代码创建http请求到localhost(相同)服务器。 – user2641906