0

我刚刚开始工作Servlets。我创建了一个动态 web 项目,我试图将其复制servlet-api.jar到 lib.xml 中。它没有成功。因此,我通过进入驱动器中的位置手动复制了工作区中的 jar,然后配置到构建路径。当我运行时tomcat server 8.0,它因一些错误而停止。当我注释掉 中的 servlet 配置时web.xml,服务器启动。配置有问题。希望以下错误有所帮助。

Java 代码

 package com.test;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;

@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

     public Servlet() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     System.out.println("In Do Get");
    response.getWriter().append("Served at: ").append(request.getContextPath());
}

  /**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("In DO Post");
    doGet(request, response);
}

}

Web.xml

<?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" version="3.1">
<display-name>Servlet</display-name>
 <welcome-file-list>
 <welcome-file>index.html</welcome-file>    
</welcome-file-list>
<servlet>
  <servlet-name>program</servlet-name>
  <servlet-class>com.test.Servlet</servlet-class>
 </servlet>
  <servlet-mapping>
    <servlet-name>program</servlet-name>
    <url-pattern>/Servlet</url-pattern>
   </servlet-mapping>
 </web-app>

错误

SEVERE: A child container failed during start
java.util.concurrent.ExecutionException:  org.apache.catalina.LifecycleException: Failed to start component  [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/Servlet]]
at java.util.concurrent.FutureTask.report(Unknown Source)
at java.util.concurrent.FutureTask.get(Unknown Source)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:916)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:871)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
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)
Caused by: org.apache.catalina.LifecycleException: Failed to start component   [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/Servlet]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153)
... 6 more
Caused by: java.lang.IllegalArgumentException: The servlets named [program] and [com.test.Servlet] are both mapped to the url-pattern [/Servlet] 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.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
4

2 回答 2

0

program名为 [ ] 和 [ ]的 servletcom.test.Servlet都映射到/Servlet不允许的 url-pattern [ ]

删除有冲突的 servlet

  <servlet-mapping>
    <servlet-name>program</servlet-name>
    <url-pattern>/Servlet</url-pattern>
   </servlet-mapping>
于 2016-07-22T17:15:20.477 回答
0

Solved the issue by removing "@WebServlet("/Servlet")" in Java code as tomcat was checking both servlets with the same name.

于 2016-07-22T17:16:30.373 回答