IllegalArgumentException:名为[Test]和[com.srk.pkg.MyServlet]的servlet都映射到url-pattern [/ MyServlet],这是不允许的

时间:2015-06-24 20:17:53

标签: eclipse tomcat servlets web.xml

在localhost启动Tomcat v8.0服务器遇到了问题。

当web应用程序的项目web.xml包含servlet信息时,

apache无法启动:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>WebApp-01</display-name>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>Test</servlet-name>
    <servlet-class>com.srk.pkg.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Test</servlet-name>
    <url-pattern>/MyServlet</url-pattern>
</servlet-mapping>

</web-app>

enter image description here

当从web.xml中删除servlet信息时,它就可以了,app部署,浏览器打开......

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>WebApp-01</display-name>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

服务器日志:

Caused by: java.lang.IllegalArgumentException: The servlets named [Test] and [com.srk.pkg.MyServlet] are both mapped to the url-pattern [/MyServlet] which is not permitted
    at org.apache.tomcat.util.descriptor.web.WebXml.addServletMapping(WebXml.java:308)
    at org.apache.catalina.startup.ContextConfig.processAnnotationWebServlet(ContextConfig.java:2325)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2007)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1901)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1896)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1896)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1896)
    at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1139)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:771)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:305)
    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:5095)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 6 more

2 个答案:

答案 0 :(得分:1)

我认为你不能定义两次相同的servlet映射:在注释和web.xml中。

当你为servlet类添加@WebServlet注释时,你不必再将它放在web.xml中。带有该注释的Servlet会自动启动。

答案 1 :(得分:-1)

有时这个问题也可以通过删除服务器并添加服务器来解决。删除服务器时,请不要忘记您还必须删除服务器的“运行时环境”。

希望这会有所帮助:)