Tomcat错误:根元素后面的文档中的标记必须格式正确

时间:2013-01-24 15:09:40

标签: java xml tomcat

我被这个错误卡住了?!我有一个简单的XML文件和一个简单的java文件。

我的xml:

<web-app xmlns="http://java.sun.com/xml/ns/javaee">
<servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>test.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello/</url-pattern>
</servlet-mapping>
</web-app>

我的java是:

package test;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;

public class HelloServlet extends HttpServlet {

public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
PrintWriter out = res.getWriter();
out.println("Hello, world!");
out.close();
}
}

我的xml放在WEB-INF中。 当我做localhost:8080时,我得到了tomcat页面。

有人可以帮助我吗?

Jan 24, 2013 3:48:58 PM org.apache.catalina.startup.ContextConfig parseWebXml
SEVERE: Parse error in application web.xml file at jndi:/localhost/opdracht-les1/WEB-INF/web.xml
org.xml.sax.SAXParseException; systemId: jndi:/localhost/opdracht-les1/WEB-INF/web.xml; lineNumber: 5; columnNumber: 2; The markup in the document following the root element must be well-formed.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$TrailingMiscDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at org.apache.tomcat.util.digester.Digester.parse(Digester.java:1537)
    at org.apache.catalina.startup.ContextConfig.parseWebXml(ContextConfig.java:1878)
    at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1259)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:878)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:369)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5173)
    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:633)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:977)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1655)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Jan 24, 2013 3:48:58 PM org.apache.catalina.startup.ContextConfig parseWebXml
SEVERE: Occurred at line 5 column 2
Jan 24, 2013 3:48:58 PM org.apache.catalina.startup.ContextConfig configureStart
SEVERE: Marking this application unavailable due to previous error(s)
Jan 24, 2013 3:48:58 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error getConfigured
Jan 24, 2013 3:48:58 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/opdracht-les1] startup failed due to previous errors
Jan 24, 2013 3:53:09 PM org.apache.catalina.startup.HostConfig deleteRedeployResources
INFO: Undeploying context [/opdracht-les1]
Jan 24, 2013 3:53:29 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive E:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\opdracht-les1.war

1 个答案:

答案 0 :(得分:0)

Java Servlets的部署描述符需要更多的命名空间声明:

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">