【问题标题】:IllegalArgumentException: The servlets named [myservlet] and [MyServlet] are both mapped to the url-pattern [/MyServlet] which is not permitted [duplicate]IllegalArgumentException:名为 [myservlet] 和 [MyServlet] 的 servlet 都映射到不允许的 url 模式 [/MyServlet] [重复]
【发布时间】:2016-03-30 20:05:38
【问题描述】:

我在 Eclipse Mars(4.5.1) 中编写了非常简单的动态 Web 应用程序,但我无法从 Eclipse 启动 tomcat 服务器。以下是错误的根本原因:

Caused by: java.lang.IllegalArgumentException: The servlets named [myservlet] and [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:2342)
      at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2024)
      at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1918)
      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:95)
      at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5154)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      ... 6 more

我按照以下步骤操作 - 在 Properties -> Java Build Path -> Add Libraries -> Server runtime -> Apache 中添加库。之后我添加了 Windows -> Preferences -> Serevr -> Runtime Environments -> Apache。 Tomcat v8.0 Server 完美从 **C:\Program Files\Apache Software Foundation\Tomcat 8.0\bin ** 开始。

我的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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>2J2EEProcessingFromData</display-name>

  <servlet>
    <servlet-name>myservlet</servlet-name>
    <servlet-class>Testing</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>myservlet</servlet-name>
    <url-pattern>/MyServlet</url-pattern>
  </servlet-mapping>


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

MyServlrt.java

public class MyServlet extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    response.setContentType("text/html;charset=ISO-8859-1");
    PrintWriter pw = response.getWriter();
    try{
        String username = request.getParameter("username");
        String password = request.getParameter("pass");

        pw.write("Hello "+username);
        pw.write("Your password is:"+password);
    } catch(Exception e){
        e.printStackTrace();
    } finally {
        pw.close();
    }
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    PrintWriter pw = response.getWriter();
    pw.write("doGet called");
    processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    PrintWriter pw = response.getWriter();
    pw.write("doPost called");
    processRequest(request, response);
}

@Override
public String getServletInfo(){
    return "Shord description";
}

}

【问题讨论】:

标签: servlets mapping url-pattern


【解决方案1】:

这很有趣,因为我正在回答我自己的问题,我将 url-pattern 替换为 (.java) 扩展名,如下所示。

<url-pattern>/MyServlet.java</url-pattern>
<url-pattern>/MyServlet</url-pattern>

【讨论】:

    【解决方案2】:

    点击Servers选项卡双击Tomcat,然后将Ports部分中的HTTP port更改为任何其他。 或打开Server.xml 文件并更改连接器端口。重启服务器然后检查

    【讨论】:

    • 你的意思是属性 -> 服务器?
    • 是的。试试那个,否则试试第二个选项
    • 同一个项目在我导入的另一个 Eclipse 工作区中工作,但在新工作区中不起作用。如果这是另一个想法,请提出建议。谢谢你 +1 已添加。
    • 谢谢,但您是否尝试使用端口号我的意思是您是否尝试使用另一个端口号。 ?
    • 我应该在哪里更改端口号?
    猜你喜欢
    • 1970-01-01
    • 2014-09-16
    • 2015-10-04
    • 2016-11-26
    • 2013-09-19
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    • 2013-07-27
    相关资源
    最近更新 更多