Tomcat deploying the same application twice in netbeans

前端 未结 4 1501
北荒
北荒 2020-12-13 11:22

I\'m using NetBeans and Tomcat seems to be deploying in .war application twice, a double launch of the web app.

I\'ve tried both Tomcat 6 and 7 and the same result.<

4条回答
  •  不思量自难忘°
    2020-12-13 12:05

    First of all, thanks Steven! Here is a more portable version of the fix:

    /**
     * tomcat workaround bug, in development mode, if tomcat is stopped and application is not un-deployed,
     * the old application will start up again on startup, and then the new code will be deployed, leading
     * to a the app starting two times and introducing subtle bugs, when this app is stopped and in dev mode
     * remove the deployment descriptor from catalina base
     */
    private static void preventTomcatNetbeansRedeployBug(final ServletContextEvent sce) {
        final String contextPath = sce.getServletContext().getContextPath();
        final String catalinaBase = System.getProperty("catalina.base");
    
        if (StringUtil.checkValidity(contextPath, catalinaBase)
                && FrameworkContext.getInstance().isDevEnvironment()) {
            final File catalinaBaseContext = new File(catalinaBase, "conf/Catalina/localhost");
            if (catalinaBaseContext.exists() && catalinaBaseContext.canRead()) {
                final File[] contexts = catalinaBaseContext.listFiles(new FilenameFilter() {
                    @Override
                    public boolean accept(File dir, String name) {
                        return name.equals(contextPath.substring(1) + ".xml");
                    }
                });
    
                if (contexts != null && contexts.length > 0) {
                    LOG.info("Deleting core context[" + contexts[0].getAbsolutePath() + "] since we are in dev");
                    contexts[0].delete();
                }
            }
        }
    }
    

    PS: substitute unknown references for your own version of it :)

    Call this custom method from the contextDestroyed method of your ServletContextListener implementation.

提交回复
热议问题