IllegalArgumentException:名为 [Hello] 和 [com.crunchify.jsp.servlet.HelloCrunchify] 的 servlet 都映射到 url 模式

IllegalArgumentException: The servlets named [Hello] and [com.crunchify.jsp.servlet.HelloCrunchify] are both mapped to the url-pattern

我已经在 eclipse 中创建了一个动态 Web 项目,如本文档中所述:sampledoc

当我 运行 服务器中的程序时,我在控制台中收到此错误:

Caused by: java.lang.IllegalArgumentException: The servlets named [Hello] and [com.crunchify.jsp.servlet.HelloCrunchify] are both mapped to the url-pattern [/CrunchifyServlet] 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:2373)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2055)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1940)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1934)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1934)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1934)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1934)
    at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1147)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:779)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:306)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:95)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5150)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    ... 6 more

我尝试从“服务器”选项卡中删除服务器并重新添加。清理了一个项目。似乎没有什么能解决问题。

我的 web.xml 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>CrunchifyJSPServletExample</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>Hello</servlet-name>
    <servlet-class>com.crunchify.jsp.servlet.HelloCrunchify</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Hello</servlet-name>
    <url-pattern>/CrunchifyServlet</url-pattern>
</servlet-mapping>

我正在使用 Tomcat 8,我的 java 主页设置为“javac 1.8.0_05” . 请帮忙!!!

您遗漏了一些相关信息,您的其他servlet 映射。 错误说明了一切:

Caused by: java.lang.IllegalArgumentException: The servlets named [Hello] and [com.crunchify.jsp.servlet.HelloCrunchify] are both mapped to the url-pattern [/CrunchifyServlet] which is not permitted

您有两个 servlet 映射,映射到同一个 URI。 尝试将 /CrunchifyServlet 的 URI 更改为 /Crunchify

<servlet>
    <servlet-name>Hello</servlet-name>
    <servlet-class>com.crunchify.jsp.servlet.HelloCrunchify</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Hello</servlet-name>
    <url-pattern>/Crunchify</url-pattern>
</servlet-mapping>

我认为堆栈跟踪的关键部分是:

The servlets named [Hello] and [com.crunchify.jsp.servlet.HelloCrunchify] are both mapped to the url-pattern [/CrunchifyServlet] which is not permitted

您需要删除其中一个 servlet 或消除 url 模式的冲突。您是否有另一个映射到相同 url 模式的应用程序?

试试这个:

转到

C:\Program Files\Apache Software Foundation\Apache Tomcat 8.x.xx\bin

找到

catalina.bat

将其复制并粘贴到另一个驱动器中,因为 windows 不允许您在此处编辑。 现在用任何编辑器打开文件。

找到 noJuliConfig 和 noJuliManager,你会到达这个图像的地方 Before Editing

您可以清楚地看到 set JAVA_OTPS="Something"noJuliConfig 和 noJuliManager。

现在您只需删除双引号,编辑后的部分将如下所示After Editing

现在只需将原来的 catalina.bat 替换为这个编辑过的。 重新启动您的 IDE。 大功告成。

稍后你可以为此感谢我。 :D