20

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
0

7 Answers 7

38

You can do the following:

  1. import the jar file inside your class:

    Old Way: import javax.servlet.http.HttpServletResponse

    New Way: import jakarta.servlet.http.HttpServlet

  2. Add the Apache Tomcat library:

    Follow these steps:

  1. Right-click on your Project from Side Menu
  2. click Properties (alt + enter)
  3. Search for java Build Path
  4. Click on Libraries
  5. Click on Add library from the right side menu
  6. Choose server runtime and click next
  7. Choose Apache Tomcat v10 (any version which is available to you)
  8. Click Finish then Apply 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Even knowing to replace javax with jakarta, I would not have guessed to replace HttpServletResponse at the end with HttpServlet. How did you know to do this? I can't find an actual mapping between these namespaces anywhere, and none of the "comprehensive" guides to the javax->jakarta transition provide this information.
27

Two possible issues could be

  • you either forgot to include Servlet jar in your classpath
  • you forgot to import it in your Servlet class

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 

1 Comment

it may be best to point to the application servers runtime instead of pulling in servlet.jar
23

Select Tomcat server in Targeted Runtime

Project->Properties->Targeted Runtimes (Select your Tomcat Server)

1 Comment

Thank you! It works! Choose e.g. for javax.servlet-api-3.1.0 the TomCat 8.0
7

Project > Properties > Java Build Path > Libraries > Add library from library tab > Choose server runtime > Next > choose Apache Tomcat v 7.0> Finish > Ok

Comments

5

if you are using maven:

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

Comments

3

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.

  1. Windows > Preferences
  2. Server > Runtime Environment
  3. Add
  4. Select your appropriate environment, click Next
  5. Point to the install directory and click Finish.

enter image description here

enter image description here

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.