1

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?

venky
  • 2,141
  • 3
  • 16
  • 20

1 Answers1

2

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.

Ian Lovejoy
  • 376
  • 4
  • 7
  • 3
    Might want to state it more emphatically, as in, *DON'T* include `javax.servlet-api-3.1.0.jar`. You should *NEVER* include it in the `.war` file. – Andreas Mar 12 '16 at 05:51
  • @Andreas haha yes, good suggestion, I guess I am sometimes polite to the point of obscuring my point. Perhaps a middle ground - "you _should not_..." – Ian Lovejoy Mar 12 '16 at 05:54
  • Okay. How do I prevent it from being included in the war? – venky Mar 12 '16 at 05:54
  • 1
    @venky That depends on how it got in there in the first place. You haven't shared your build script. If using Maven, see this question: http://stackoverflow.com/questions/6646959/difference-between-maven-scope-compile-and-provided-for-jar-packaging – Andreas Mar 12 '16 at 05:55
  • @venky Assuming you are building the .war from a directory, remove it from that source directory. – Ian Lovejoy Mar 12 '16 at 05:55
  • @Andreas : Added the pom.xml file. I am using maven for generating it. – venky Mar 12 '16 at 05:58
  • 2
    @venky Then add `provided`. – Andreas Mar 12 '16 at 05:58