When creating a new Java Servlet in Eclipse IDE, you may encounter the following errors:

HttpServlet cannot be resolved to a type
HttpServletRequest cannot be resolved to a type
HttpServletResponse cannot be resolved to a type
ServletException cannot be resolved to a type
The import javax.servlet cannot be resolved
WebServlet cannot be resolved to a type
It looks something like this screenshot in Eclipse:

Error httpservlet cannot be resolved to a type

The reason is the Java Servlet API is missing in the project’s classpath. You can solve this problem by specifying a server runtime for the project, e.g. Apache Tomcat runtime – because a Java web server is a servlet container that implements the Servlet API.

In Eclipse, right click on the project, click Properties. In the project properties dialog, click Java Build Path, and click the button Add Library… as shown below:

Java Build Path in Eclipse

In the Add Library dialog appears, select Server Runtime:

Add Library in Eclipse

Click Next. In the next screen select Apache Tomcat and click Finish:



server library tomcat

If you don’t see any Apache Tomcat servers here, probably you haven’t installed Tomcat nor added to Eclipse. So follow this tutorial to add Tomcat in Eclipse.

Click Finish to close the Server Library dialog, and you will see the server library Apache Tomcat appears:

Apache Tomcat in Libraries

Then click Apply and Close, the errors will be gone.

In case you use Maven, the solution would be simpler. Just add a dependency for Java Servlet API in the pom.xml file like this:

<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>javax.servlet-api</artifactId>
	<version>3.0.1</version>
	<scope>provided</scope>
</dependency>
Save the file and wait for seconds for Maven to update the project. You will see the errors are gone way.

 

NOTE: To avoid this error in future, you should select the Target Runtime as Apache Tomcat when creating the project in Eclipse:

Target Runtime Apache Tomcat

 

Other Java Servlet Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment

   


Comments 

#21kash2024-04-02 23:26
Thank you so much my program was able to run because of really appreciate you work
Quote
#20ilyasj2024-02-29 09:55
Thank you man fixed it
Quote
#19Varun2023-05-14 17:10
I have tomcat installed but i don't see the option called server runtime.
Quote
#18Nishit2022-09-26 14:01
Thanks its grt saving time and quick solution
Quote
#17Lee2022-09-02 07:09
To the Point! Great, Thanks!
Quote