我是Tomcat的新手,以下问题使我很难受。因此,我完成了一个非常简单的servlet项目,该项目可用于glassfish,jetty和wildfly,但是当我将其部署为war文件时,它不适用于tomcat(它可用于外部上下文部署)。
当我启动tomcat时,我使用管理器来部署war文件,并且一切正常,我可以毫无问题地访问新部署的servlet。当我关闭tomcat并重新启动它时(在从xampp进行正常的关闭过程或完全重新启动PC之后),问题就开始了。在那之后,使用先前的war文件,我尝试在localhost:8080上启动任何内容,并且没有页面在工作。甚至没有欢迎页面,管理器页面或servlet本身,所有这些都给了我错误状态404。
在日志中,我收到以下错误消息
严重:结束事件引发异常java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addServlet位于 org.apache.tomcat.util.IntrospectionUtils.callMethod1(IntrospectionUtils.java:849) 在 org.apache.tomcat.util.digester.SetNextRule.end(SetNextRule.java:201) 在 org.apache.tomcat.util.digester.Digester.endElement(Digester.java:1063) 在 com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:609) 在 com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782) 在 com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl $ FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2967) 在 com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602) 在 com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505) 在 com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:842) 在 com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771) 在 com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141) 在 com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213) 在 com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl $ JAXPSAXParser.parse(SAXParserImpl.java:643) 在org.apache.tomcat.util.digester.Digester.parse(Digester.java:1561) 在 org.apache.catalina.startup.ContextConfig.parseWebXml(ContextConfig.java:1825) 在 org.apache.catalina.startup.ContextConfig.getDefaultWebXmlFragment(ContextConfig.java:1480) 在 org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1257) 在 org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:889) 在 org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:386) 在 org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117) 在 org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90) 在 org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5380) 在 org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 在 org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) 在 org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) 在 org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649) 在 org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1083) 在 org.apache.catalina.startup.HostConfig $ DeployWar.run(HostConfig.java:1879) 在 java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:511) 在java.util.concurrent.FutureTask.run(FutureTask.java:266)在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 在 java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624) 在java.lang.Thread.run(Thread.java:748)
2018年12月12日下午2:11:11 org.apache.catalina.startup.ContextConfig parseWebXml严重:在以下位置解析应用程序web.xml文件中的错误 文件:/ C:/xampp/tomcat/conf/web.xml org.xml.sax.SAXParseException; systemId:文件:/ C:/xampp/tomcat/conf/web.xml; lineNumber:114; columnNumber:15;错误发生在(114,15): org.apache.catalina.deploy.WebXml addServlet位于 org.apache.tomcat.util.digester.Digester.createSAXException(Digester.java:2711) 在 org.apache.tomcat.util.digester.Digester.createSAXException(Digester.java:2743) 在 org.apache.tomcat.util.digester.Digester.endElement(Digester.java:1066) 在 com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:609) 在 com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782) 在 com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl $ FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2967) 在 com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602) 在 com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505) 在 com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:842) 在 com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771) 在 com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141) 在 com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213) 在 com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl $ JAXPSAXParser.parse(SAXParserImpl.java:643) 在org.apache.tomcat.util.digester.Digester.parse(Digester.java:1561) 在 org.apache.catalina.startup.ContextConfig.parseWebXml(ContextConfig.java:1825) 在 org.apache.catalina.startup.ContextConfig.getDefaultWebXmlFragment(ContextConfig.java:1480) 在 org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1257) 在 org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:889) 在 org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:386) 在 org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117) 在 org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90) 在 org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5380) 在 org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 在 org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) 在 org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) 在 org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649) 在 org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1083) 在 org.apache.catalina.startup.HostConfig $ DeployWar.run(HostConfig.java:1879) 在 java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:511) 在java.util.concurrent.FutureTask.run(FutureTask.java:266)在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 在 java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624) 在java.lang.Thread.run(Thread.java:748)造成原因: java.lang.NoSuchMethodException:org.apache.catalina.deploy.WebXml 在的addServlet org.apache.tomcat.util.IntrospectionUtils.callMethod1(IntrospectionUtils.java:849) 在 org.apache.tomcat.util.digester.SetNextRule.end(SetNextRule.java:201) 在 org.apache.tomcat.util.digester.Digester.endElement(Digester.java:1063) ...再加30
这是我的servlet代码:
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'
我试图重新安装xampp,以创建一个新项目,并通过自己在Webapps中复制战争(而不使用管理器)进行部署。该servlet可以完美地适用于所有内容,包括tomcat嵌入式和jetty嵌入式,这也是我使用外部上下文进行部署时所说的方式,但仅通过部署.war文件,它就不能仅在tomcat上运行。除了部署战争并添加具有manager-gui角色的用户外,我在tomcat中没有进行任何其他更改。其他所有内容都是xampp的tomcat的默认设置。