Are you getting “HttpServlet cannot be resolved to a type” while running your java program while starting Tomcat Server? Yes, I’m referring to this project: https://crunchify.com/how-to-run-java-program-automatically-on-tomcat-startup/
But you may see above mentioned exception while running this program.
You need to add the Servlet API to your classpath. In Tomcat 6.0, this is in a JAR called servlet-api.jar
in Tomcat’s lib
folder. You can either add a reference to that JAR to the project’s classpath, or put a copy of the JAR in your Eclipse project and add it to the classpath from there.
If you want to leave the JAR in Tomcat’s lib
folder:
- Right-click the project, click Properties.
- Choose Java Build Path.
- Click Add External JARs…
- Browse to find
servlet-api.jar
and select it. - Click OK to update the build path.
Or, if you copy the JAR into your project:
- Right-click the project, click Properties.
- Choose Java Build Path.
- Click Add JARs…
- Find
servlet-api.jar
in your project and select it. - Click OK to update the build path.
If you have a Maven Project then add below dependency:
<dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency>