title: Tomcat casting servlets to javax.servlet.Servlet instead of jakarta.servlet.http.HttpServlet
body: <p>I have been  trying in vain to implement tomcat 9's jakarta servlet as opposed to the previous javax.servlet implementation (as its my understanding that the jakarta package is the way forward).  The issue is that when I point the browser at my servlet's url I get the following error(s)...</p>
  <pre><code>java.lang.ClassCastException: class cti.nm.web.Index cannot be cast to class javax.servlet.Servlet (cti.nm.web.Index is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @48c76607; javax.servlet.Servlet is in unnamed module of loader java.net.URLClassLoader @621be5d1)
      org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
      org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
      org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
      org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
      org.apache.coyote.ajp.AjpProcessor.service(AjpProcessor.java:432)
      org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
      org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:888)
      org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1597)
      org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
      java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
      java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
      org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      java.base/java.lang.Thread.run(Thread.java:832)
  </code></pre>
  <p>The problem is obvious enough.  Tomcat is trying to cast my jakarta.servlet.http.HttpServlet to a javax.servlet.Servlet which clearly won't work.  What I can't figure is how to tell it what class the servlet is actually implementing.</p>
  <p>The class itself is declared as ...</p>
  <pre><code>package cti.nm.web;
  import jakarta.servlet.http.HttpServlet;
  import java.io.IOException;
  import java.io.PrintWriter;
  import jakarta.servlet.ServletException;
  import jakarta.servlet.http.HttpServletRequest;
  import jakarta.servlet.http.HttpServletResponse;
  
  public class Index extends HttpServlet {
       
      public void doGet(HttpServletRequest request,
                        HttpServletResponse response)
        throws IOException, ServletException {
  
          response.setContentType(&quot;text/html&quot;);
         //print a bunch of stuff
      }
  }
  
  </code></pre>
  <p>My web.xml file is as follows ...</p>
  <pre><code>&lt;web-app xmlns=&quot;https://jakarta.ee/xml/ns/jakartaee&quot;
           xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
           xsi:schemaLocation=&quot;https://jakarta.ee/xml/ns/jakartaee
                               https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd&quot;
           version=&quot;5.0&quot;&gt;
           
      &lt;display-name&gt;NMWeb&lt;/display-name&gt;
      &lt;description&gt;
         NMWeb Description
      &lt;/description&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;Index&lt;/servlet-name&gt;
          &lt;servlet-class&gt;cti.nm.web.Index&lt;/servlet-class&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Index&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/NMWeb&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
  &lt;/web-app&gt;  
  </code></pre>
  <p>I had hoped that using the proper jakartaee xml schema in the deployment descriptor would cause the correct clases to be used but apparently not.  The WAR seems to contain the proper jars ...</p>
  <p>jakarta.jakartaee-api-9.0.0.jar<br />
  tomcat-el-api-10.0.0.jar<br />
  tomcat-servlet-api-10.0.0.jar
  jakarta.servlet-api-5.0.0.jar<br />
  tomcat-jsp-api-10.0.0.jar</p>
  <p>The pom is specified as ...</p>
  <pre><code>&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;cti.nm.NMWeb&lt;/groupId&gt;
    &lt;artifactId&gt;NMWeb&lt;/artifactId&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
    &lt;packaging&gt;war&lt;/packaging&gt;
  
    &lt;build&gt;
      &lt;sourceDirectory&gt;src&lt;/sourceDirectory&gt;
      &lt;plugins&gt;
        &lt;plugin&gt;
          &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
          &lt;version&gt;3.8.1&lt;/version&gt;
          &lt;configuration&gt;
            &lt;source&gt;1.8&lt;/source&gt;
            &lt;target&gt;1.8&lt;/target&gt;
          &lt;/configuration&gt;
        &lt;/plugin&gt;
        &lt;plugin&gt;
          &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
          &lt;version&gt;3.2.3&lt;/version&gt;
          &lt;configuration&gt;
            &lt;warSourceDirectory&gt;WebContent&lt;/warSourceDirectory&gt;
          &lt;/configuration&gt;
        &lt;/plugin&gt;
      &lt;/plugins&gt;
    &lt;/build&gt;
    &lt;dependencies&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;jakarta.platform&lt;/groupId&gt;
        &lt;artifactId&gt;jakarta.jakartaee-api&lt;/artifactId&gt;
        &lt;version&gt;[9.0.0,)&lt;/version&gt;
        
      &lt;/dependency&gt;
      &lt;!-- https://mvnrepository.com/artifact/jakarta.servlet/jakarta.servlet-api --&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;jakarta.servlet&lt;/groupId&gt;
        &lt;artifactId&gt;jakarta.servlet-api&lt;/artifactId&gt;
        &lt;version&gt;[5.0.0,)&lt;/version&gt;
        
      &lt;/dependency&gt;
          &lt;!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jsp-api --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
              &lt;artifactId&gt;tomcat-jsp-api&lt;/artifactId&gt;
              &lt;version&gt;[10.0.0,)&lt;/version&gt;
         
          &lt;/dependency&gt;
    &lt;/dependencies&gt;
  &lt;/project&gt;
  </code></pre>
  <p>It's been days of searching the web for answers to this question and I can't seem to find any other instances of this exact scenario.  I should mention that this is an eclipse generated probject that I have manually modified.  Building with both maven and eclipse generates the same results when I deploy the war file.</p>