0

I am using Eclipse Version: 2021-12 (4.22.0). Tomcat 10.0.13. Trying a simple hello world. But I am getting error

SEVERE: Allocate exception for servlet [bharpur] java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet

enter image description here

I have added

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

in pom.xml and

it is also present in Maven Dependencies. javax.servlet-api-4.0.1.jar

I am still getting error

SEVERE: Allocate exception for servlet [bharpur]
java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet

This is my servlet

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  
  <servlet>
    <servlet-name>bharpur</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>bharpur</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

This is my controller

package com.bharpur;
@Controller
public class AddController {
    
    @RequestMapping("/add")
    public void add() {
        System.out.prinln("Add Controller");
    }
}

index.jsp

<html>
<body>
<form action="add">
<input type="text" name="t1"><br>
<input type="text" name="t2"><br>
<input type="submit">
</form>
</body>
</html> 

How can i remove little cross over project. I have already added targeted runtime from project->properties.

Honey
  • 43
  • 7
  • 2
    Short answer: Servlet API 4.0 is not compatible with Tomcat 10. Either use Servlet API 5.0 (and modify your servlet code to use jakarta package, or use Tomcat 9 or earlier. – Stephen C Mar 14 '22 at 07:26
  • @StephenC I modified my project and added Servlet API 5.0 in pom.xml, done Maven update. It still shows the same error. – Honey Mar 15 '22 at 13:10
  • You probably still have Servlet 4.0 in the transitive dependencies. You need to track down what is causing that and eliminate it. `javax.servlet.*` APIs are not present on Tomcat 10. – Stephen C Mar 15 '22 at 13:40
  • 1
    Actually, I think this is not going to work. According to https://spring.io/blog/2021/09/02/a-java-17-and-jakarta-ee-9-baseline-for-spring-framework-6, Spring is currently still on Java EE 7-8. That's not going to work with Tomcat 10. You need to downgrade to Tomcat 9 for now. You will need to use Servlet 4.0 ... on Tomcat 9. – Stephen C Mar 15 '22 at 13:47
  • This is all a consequence of Oracle's "off-loading" of Java EE to the Apache Foundation ... and Oracle's insistence that the package `javax` no longer be used. It has caused pain all over the place. – Stephen C Mar 15 '22 at 13:49
  • Thanks @StephenC you are right I degraded to Tomcat 9 it works fine now. – Honey Mar 17 '22 at 04:10

0 Answers0