18

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
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
BAlu BAlaji
  • 193
  • 1
  • 1
  • 4

7 Answers7

37

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.

princebansal_
  • 133
  • 3
  • 9
Areeg Samir
  • 577
  • 5
  • 6
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
Community
  • 1
  • 1
PermGenError
  • 45,977
  • 8
  • 87
  • 106
22

Select Tomcat server in Targeted Runtime

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

Chinmoy
  • 1,391
  • 13
  • 14
8

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

Lokesh Tiwari
  • 81
  • 1
  • 1
4

if you are using maven:

<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
</dependency>
Brimstedt
  • 3,020
  • 22
  • 32
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

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
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.

ÖMER TAŞCI
  • 546
  • 5
  • 9