1
votes

I have a simple Java servlet project which I am running via maven tomcat plugin.Hitting the url, I get this error

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

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

The servlet class is

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();
    }
    .
    .
    .
}

This is accompanied by a simple web.xml file.

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

I have cross checked the war file generated. I am attaching the output of tree command on the war file.

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

I am building this using maven. The following is the pom.xml file.

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

Even though the class MyServlet is implementing a javax Servlet, I am getting this error.

What am I doing wrong?

1
Can you please paste the import section please ? - Sarker
What if you do instead public class MyServlet extends HttpServlet { - Sarker
@Sarker : Added import section. It gives the same error if I extend HttpServlet. - venky
Just remove javax.servlet-api-3.1.0.jar from your .war file as specified in the answer by IanLovejoy, and your problem will go away. - Andreas

1 Answers

2
votes

You typically will not want to include javax.servlet-api-3.1.0.jar in your .war. It will be provided by the framework. If two classes are loaded by different class loaders, even if they have the same package and name, they will be considered distinct classes.