0

I am new to Tomcat and the following problem is giving me a hard time. So I've done a really simple servlet project, which works on glassfish, jetty and wildfly, but it doesn't work on tomcat when I deploy it as a war file (it works with an external context deployment).

When I start tomcat, I use the manager to deploy the war file, and everything works, I can access the newly deployed servlet without any problems. The problems start when I close tomcat and start it again (after a normal closing procedure from xampp or restarting the pc entirely). After that, with the previous war file working, I try to start anything on localhost:8080, and no page is working. Not even the welcome page, the manager page or the servlet itself, everything giving me Error status 404.

In the logs I receive the following errors

SEVERE: End event threw exception java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addServlet at org.apache.tomcat.util.IntrospectionUtils.callMethod1(IntrospectionUtils.java:849) at org.apache.tomcat.util.digester.SetNextRule.end(SetNextRule.java:201) at org.apache.tomcat.util.digester.Digester.endElement(Digester.java:1063) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:609) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2967) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:842) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643) at org.apache.tomcat.util.digester.Digester.parse(Digester.java:1561) at org.apache.catalina.startup.ContextConfig.parseWebXml(ContextConfig.java:1825) at org.apache.catalina.startup.ContextConfig.getDefaultWebXmlFragment(ContextConfig.java:1480) at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1257) at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:889) at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:386) at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117) at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5380) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649) at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1083) at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1879) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)

Dec 12, 2018 2:11:11 PM org.apache.catalina.startup.ContextConfig parseWebXml SEVERE: Parse error in application web.xml file at file:/C:/xampp/tomcat/conf/web.xml org.xml.sax.SAXParseException; systemId: file:/C:/xampp/tomcat/conf/web.xml; lineNumber: 114; columnNumber: 15; Error at (114, 15) : org.apache.catalina.deploy.WebXml addServlet at org.apache.tomcat.util.digester.Digester.createSAXException(Digester.java:2711) at org.apache.tomcat.util.digester.Digester.createSAXException(Digester.java:2743) at org.apache.tomcat.util.digester.Digester.endElement(Digester.java:1066) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:609) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2967) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:842) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643) at org.apache.tomcat.util.digester.Digester.parse(Digester.java:1561) at org.apache.catalina.startup.ContextConfig.parseWebXml(ContextConfig.java:1825) at org.apache.catalina.startup.ContextConfig.getDefaultWebXmlFragment(ContextConfig.java:1480) at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1257) at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:889) at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:386) at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117) at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5380) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649) at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1083) at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1879) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addServlet at org.apache.tomcat.util.IntrospectionUtils.callMethod1(IntrospectionUtils.java:849) at org.apache.tomcat.util.digester.SetNextRule.end(SetNextRule.java:201) at org.apache.tomcat.util.digester.Digester.endElement(Digester.java:1063) ... 30 more

Here is my servlet code:

LoginSuccesAdmin

@WebServlet(name = "succesAdmin", urlPatterns = { "/succesAdmin" })
public class LoginSuccesAdmin extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doPost(req, resp);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        String usr = request.getParameter("userName");
        String pass = request.getParameter("userPass");
        System.out.println(usr + " " + pass);
        out.print("Welcome " + usr + " ");
        out.print("Your password is: " + pass);
        response.setStatus(200);
        out.close();
    }

}

LoginSuccesServlet

@WebServlet(name = "succesServlet", urlPatterns = { "/succesServlet" })
public class LoginSuccesServlet extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doPost(req, resp);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        String usr = request.getParameter("userName");
        System.out.println(usr);
        out.print("Welcome " + usr);
        response.setStatus(200);
        out.close();
    }

}

LoginUser

@WebServlet(name = "loginServlet", urlPatterns = { "/loginServlet" })
public class LoginUser extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        doPost(req, resp);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<html>\n" +
                "  <form action=\"loginServlet\" method=\"post\">");
        out.println("Name:<input type=\"text\" name=\"userName\"/><br/>\n" +
                "      Password:<input type=\"password\" name=\"userPass\"/><br/>\n" +
                "      <input type=\"submit\" name=\"press\" value=\"login\"/>");
        out.println("  </form>\n" +
                "</html>");

        String press = request.getParameter("press");
        if (press == null) {
            //no button has been selected
        } else if (press.equals("login")) {
            String userName = request.getParameter("userName");
            String userPass = request.getParameter("userPass");

            if (userName.equals("servlet") && userPass.equals("servlet")) {
                RequestDispatcher rd=request.getRequestDispatcher("succesServlet");
                if(rd == null) {
                    response.sendError(404);
                    return;
                }
                rd.forward(request, response);
            } else if (userName.equals("admin") && userPass.equals("admin")) {
                RequestDispatcher rd=request.getRequestDispatcher("succesAdmin");
                if(rd == null) {
                    response.sendError(404);
                    return;
                }
                rd.forward(request, response);
            } else {
                out.print("Wrong username or password");
            }
        }
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1"
         metadata-complete="false">
</web-app>

build.gradle

    plugins {
    id 'java'
    id 'war'
    id 'application'
}

group 'LoginServletTJAP'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    providedCompile 'javax.servlet:javax.servlet-api:+'
    compile 'org.eclipse.jetty:jetty-server:9.0.0.RC2'
    compile 'org.eclipse.jetty:jetty-servlet:9.0.0.RC2'
    compile 'org.apache.tomcat:tomcat-catalina:7.0.41'
    compile 'org.apache.tomcat:tomcat-util:7.0.41'
    compile 'org.apache.tomcat.embed:tomcat-embed-core:7.0.41'
}


sourceSets {
    main {
        java {
            srcDirs = ['src']
        }
    }
}

war {
    webInf { from 'src' } // adds a file-set to the WEB-INF dir.
    webXml = file('web/WEB-INF/web.xml') // copies a file to WEB-INF/web.xml
}

war.doLast {
    ant.unzip(src: war.archivePath, dest: "$buildDir/exploded")
}

jar {
    manifest {
        attributes(
                'Main-Class': 'Main'
        )
    }
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

task(selectServer, dependsOn: 'classes', type: JavaExec) {
    main = 'Main'
    classpath = sourceSets.main.runtimeClasspath
    standardInput = System.in
}

defaultTasks 'selectServer'

I tried to reinstall xampp, to create a new project, to deploy by copying the war in the webapps myself (not using the manager). The servlet works perfectly on everything, including tomcat embedded and jetty embedded, also how I said by deploying using an external context, but it doesn't work only on tomcat by simply deploying the .war file. I haven't changed anything else in tomcat except deploying the war and adding an user with the manager-gui role. Everything else are the default settings of xampp's tomcat.

Gopal Bhuva
  • 654
  • 2
  • 13
  • 20
Artyomska
  • 1,309
  • 5
  • 26
  • 53
  • 1
    Hi - is the tomcat version the same as the version specified in your gradle file? – robot_alien Dec 12 '18 at 12:36
  • Now that I looked at it, the versions were different. But I just tried to redeploy with the same version in gradle as in the xampp's tomcat (7.0.56), and I still receive exactly the same thing :/ – Artyomska Dec 12 '18 at 12:45
  • 1
    Have a look at this answer:https://stackoverflow.com/a/20517837/1004631 keep a backup of these jars elsewhere before you delete them all... And let me know – robot_alien Dec 12 '18 at 12:53
  • I have not one of those files in my lib folder. Can that be the problem? Right now in my lib file I have only what is defined it the dependecies part of the build.gradle – Artyomska Dec 12 '18 at 12:55
  • 1
    Which log gives you that error? – ZhaoGang Dec 12 '18 at 13:05
  • 1
    @Artyomska well, that's not the problem then, as you said it worked previously. Try to maintain a backup of catalina.*.jar, delete it and then check. Also, refer https://bbs.sunofbeaches.com/thread-6079-1-1.html, it mentions to add `` to your `/conf/context.xml` tomcat configuration file. – robot_alien Dec 12 '18 at 13:07
  • The errors are given inside the catalina.{logDate}.log file @robot_alien I will try that and I will report – Artyomska Dec 12 '18 at 13:13
  • 1
    @robot_alien my God the Loader delegate thing did it, now it works perfectly. I just tried all the steps and now it doesn't matter how I deploy or if I close the tomcat, the servlets work as they are supposed to. Thank you so much :DD – Artyomska Dec 12 '18 at 13:18
  • @Artyomska cool, that's great news! :) – robot_alien Dec 12 '18 at 13:19

0 Answers0