Search code examples
javatomcatservletsweb.xmlservletcontextlistener

ServletContextListener is stopping the deployment of Web Application on Apache Tomcat


I have implemented a ServletContextListener in Simple java class.Now inside this i have called a method to execute as soon as contextInitialized of ServletContextListener runs.The nature of called method is that it is complex and takes long time hours to execute. There is only one web page called index.jsp which is welcome page that needs to displayed on browser as soon as web application runs but this is not displaying as first called method executes and it takes long time to execute..

I need welcome page to display and function called in ServletContextListener to execute in background ..

Here is my ServletContextListener class..

public class Startup implements ServletContextListener
{
@Override
public void contextDestroyed(ServletContextEvent sce) {}

@Override
public void contextInitialized(ServletContextEvent sce)
{
    // Do your startup work here
     executeprocess();
}

}

and here is my web.xml file..

 <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
 <listener>
    <listener-class>org.myapp.Startup</listener-class>
</listener>

Please help me. Thanks in advance..


Solution

  • Try adding thread as below:

    @Override public void contextInitialized(ServletContextEvent sce) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            executeprocess();
        }
    }).start();}