I got an error while creating a servlet in eclipse like
Multiple markers at this line
- ServletException cannot be resolved to a type
- HttpServletResponse cannot be resolved to a type
- HttpServletRequest cannot be resolved to a type
I got an error while creating a servlet in eclipse like
Multiple markers at this line
- ServletException cannot be resolved to a type
- HttpServletResponse cannot be resolved to a type
- HttpServletRequest cannot be resolved to a type
You can do the following:
import the jar file inside your class:
Old Way: import javax.servlet.http.HttpServletResponse
New Way: import jakarta.servlet.http.HttpServlet
Add the Apache Tomcat library:
Follow these steps:
- Right-click on your Project from Side Menu
- click
Properties
(alt + enter)- Search for
java Build Path
- Click on
Libraries
- Click on
Add library
from the right side menu- Choose
server runtime
and clicknext
- Choose Apache Tomcat v10 (any version which is available to you)
- Click
Finish
thenApply Changes
Also, First of all, make sure that the Servlet jar is included in your class path in eclipse as PermGenError said.
This will solve your error.
Two possible issues could be
To include Servlet jar in your class path in eclipse, Download the latest Servlet Jar and configure using buildpath option. look at this Link for more info.
If you have included the jar make sure that your import is declared.
import javax.servlet.http.HttpServletResponse
Select Tomcat server in Targeted Runtime
Project->Properties->Targeted Runtimes (Select your Tomcat Server)
Project > Properties > Java Build Path > Libraries > Add library from library tab > Choose server runtime > Next > choose Apache Tomcat v 7.0> Finish > Ok
if you are using maven:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
Are the classes imported? Try pressing CTRL
+ SHIFT
+ O
to resolve the imports. If this does not work you need to include the application servers runtime libraries.
As a reason of this problem, some code is broken or undefined.You may see an error in a java class such as "The type javax.servlet.http.HttpSession cannot be resolved. It is indirectly referenced from required .class files". you shuold download " javax.servlet.jar" as mentioned before. Then configure your project build path, add the javax.servlet.jar as an external jar.I hope it fixes the problem.At least it worked for me.