2016-04-28 47 views
1

我想一旦服务器得到了成功启动时的通知。对于我已经添加了低于在web.xml如何在web.xml中指定LifeCycle Listener监听器类?

<listener> <listener-class>com.server.container.Listeners</listener-class> </listener> 

的侦听器类,它实现org.apache.catalina.LifecycleListener。

这是正确的吗?截至目前,我在服务器启动结束时没有收到任何通知。我需要做额外的事吗?

回答

1

在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”文件。

+0

我想在服务器启动完成后收到通知。上述contextInitialized将在启动期间调用以启动我们的上下文类。我想在完成服务器启动后发出通知。那是可能的 – user2641906

+0

我可以知道,在服务器启动后,您想要处理的确切操作是什么? – developer

+0

是的..服务器启动后,我需要通过代码创建http请求到localhost(相同)服务器。 – user2641906