在使用servlet的时候遇到一个报错

jakarta.servlet.ServletException: 类com.seig.servlet.HelloServlet不是Servlet
     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:693)
     org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:339)
     org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
     org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
     org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:845)
     org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1563)
     org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
     java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
     java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
     org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
     java.lang.Thread.run(Thread.java:748)
 根本原因。
 ​
 java.lang.ClassCastException: com.kang.servlet.HelloServlet cannot be cast to jakarta.servlet.Servlet
     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:693)
     org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:339)
     org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
     org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
     org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:845)
     org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1563)
     org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
     java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
     java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
     org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

pom.xml

<dependency>
     <groupId>javax.servlet</groupId>
     <artifactId>javax.servlet-api</artifactId>
     <version>4.0.1</version>
     <scope>compile</scope>
 </dependency>

这是因为使用了最新的tomcat10,而tomcat10有个最大的变动就是Tomcat 10 m1将JavaEE也换成了Jakarta® EE Jakarta® EE较以前的JavaEE有一个非常非常重要的更新:包名不再是"javax.servlet.“而是改成了"jakarta.servlet.” tomcat10之后servlet依赖包名不是javax.servlet,而是jakarta.servlet

可以用以下两个依赖

<dependency>
   <groupId>jakarta.servlet.jsp</groupId>
   <artifactId>jakarta.servlet.jsp-api</artifactId>
   <version>3.0.0</version>
   <scope>provided</scope>
 </dependency>
 <dependency>
   <groupId>jakarta.servlet</groupId>
   <artifactId>jakarta.servlet-api</artifactId>
   <version>5.0.0</version>
   <scope>provided</scope>
 </dependency>

解决方案:或者不使用Tomcat10,改用Tomcat9

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐