运行一个简单的java Servlet给出Class不是Servlet错误

时间:2016-03-12 05:35:20

标签: java tomcat servlets

我有一个简单的Java servlet项目,我通过maven tomcat plugin运行。点击url,我收到此错误

javax.servlet.ServletException: Class in.vshukla.MyServlet is not a Servlet

java.lang.ClassCastException: in.vshukla.MyServlet cannot be cast to javax.servlet.Servlet

servlet类是

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class MyServlet implements Servlet {
    .
    .
    .
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        PrintWriter out = servletResponse.getWriter();
        out.println("<html>");
        out.println("<body>");
        out.println("<h1>Hello, world!</h1>");
        out.println("</body>");
        out.println("</html>");
        out.close();
    }
    .
    .
    .
}

这附带一个简单的web.xml文件。

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">
     <servlet>
         <servlet-name>servlet</servlet-name>
         <servlet-class>in.vshukla.MyServlet</servlet-class>
     </servlet>
     <servlet-mapping>
         <servlet-name>servlet</servlet-name>
         <url-pattern>/*</url-pattern>
     </servlet-mapping>
</web-app>

我已经交叉检查了生成的war文件。我在war文件上附加了tree命令的输出。

.
|-- META-INF
`-- WEB-INF
    |-- classes
    |   |-- in
    |   |   `-- vshukla
    |   |       `-- MyServlet.class
    |   `-- spring
    |       `-- spring_config.xml
    |-- lib
    |   |-- javax.servlet-api-3.1.0.jar
    |   `-- my-data-0.1-SNAPSHOT.jar
    `-- web.xml

7 directories, 5 files

我正在使用maven构建它。 以下是pom.xml文件。

<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

  <parent>
    <groupId>in.vshukla</groupId>
    <artifactId>my</artifactId>
    <version>0.1-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <artifactId>my-api</artifactId>
  <packaging>war</packaging>

  <properties>
    <javax.version>3.1.0</javax.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>in.vshukla</groupId>
      <artifactId>my-data</artifactId>
      <version>${project.parent.version}</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>${javax.version}</version>
    </dependency>
  </dependencies>

</project>

即使MyServlet类正在实现javax Servlet,我也收到了这个错误。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您通常不希望在.war中包含javax.servlet-api-3.1.0.jar。它将由框架提供。如果两个类由不同的类加载器加载,即使它们具有相同的包和名称,它们也将被视为不同的类。