【问题标题】:IllegalArgumentException: The servlets named [Test] and [com.srk.pkg.MyServlet] are both mapped to the url-pattern [/MyServlet] which is not permitted [duplicate]IllegalArgumentException:名为 [Test] 和 [com.srk.pkg.MyServlet] 的 servlet 都映射到不允许的 url 模式 [/MyServlet] [重复]
【发布时间】:2015-06-24 20:17:53
【问题描述】:

在 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>

当从 web.xml 中删除 servlet 信息时,一切正常,应用程序被部署,浏览器打开...

<?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

【问题讨论】:

  • com.srk.pkg.MyServlet 类中有注释@WebServlet 吗?
  • 是的,@WebServlet("/MyServlet") public class MyServlet extends HttpServlet {......它通过删除它来工作。我不知道那个注释。还是谢谢
  • 很酷,这个注解使得 web.xml 中不需要 servlet 规范。 docs.oracle.com/javaee/6/api/javax/servlet/annotation/…

标签: eclipse tomcat servlets web.xml


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    希望这会有所帮助:)

    【讨论】:

      猜你喜欢
      • 2016-03-30
      • 1970-01-01
      • 2016-11-26
      • 2013-09-19
      • 2016-07-23
      • 2013-07-27
      • 2016-03-07
      • 1970-01-01
      • 2014-10-06
      相关资源
      最近更新 更多