88

I am developing web app using spring frame work, but i am getting following error in some of the jsp pages.

javax.servlet.ServletException cannot be resolved to a type

I have included servlet-api also but still same error message is displaying. How to over come this problem?

6 Answers6

279

I guess this may work, in Eclipse select your project → then click on project menu bar on top → goto to properties → click on Targeted Runtimes → now you must select a check box next to the server you are using to run current project → click Apply → then click OK button. That's it, give a try.

Kishan_KP
  • 4,488
  • 6
  • 27
  • 46
  • 4
    Thanks. In Eclipse Kepler you may need to toggle the check box off, apply, toggle it back on and apply again for this to take effect. – Bloodboiler Apr 08 '14 at 12:09
  • 6
    Unbelievable! This actually solved it! I'd classify this as a bug. It's extremely silly for an IDE to complain about such a problem for such a small reason which it should have taken care of by itself. What am I using an IDE for, for god's sake?!! – Nav Aug 28 '14 at 03:57
  • Using Eclipse Mars, and still this is an issue ;-). I had Tomcat 6 and 7 selected under Targeted runtimes and I had to make sure "only" Tomcat 7 is selected (that's my runtime) to get rid of this error. – rkrishnan Aug 30 '15 at 15:48
  • Thanks a ton, but could you please elaborate what exactly problem was? – Ajay Sharma Sep 23 '15 at 07:28
  • It works for me, I had to go to the EAR properties. thank you so much!! – Richard Rebeco May 02 '16 at 20:15
  • Same Problem, but this dind't work. Tomcat 8 was already selected and uncheck/apllay/check/apply dind't help. Any ideas? – Tobi123 Jul 17 '16 at 13:20
  • 1
    Thanks. It worked for me. I don't know why other persons got the error but in my case I was messing around the server. I deleted it and made it from scratch again. I had 12 errors in the beginning. Cleaning removed half of it but 6 remained even after a reboot (actually a boot from yesterday to today). Now this has solved my issue. NB: Even before applying this my code was working if I ran the server. – 猫IT May 21 '19 at 20:05
  • It worked for me...thank you sooo mucchhhh dude!!!!!!!!!!!1 – Mutaealim Dec 06 '20 at 09:57
27

It seems to me that eclipse doesn't recognize the java ee web api (servlets, el, and so on). If you're using maven and don't want to configure eclipse with a specified server runtime, put the dependecy below in your web project pom:

<dependency>  
    <groupId>javax</groupId>    
    <artifactId>javaee-web-api</artifactId>    
    <version>7.0</version> <!-- Put here the version of your Java EE app, in my case 7.0 -->
    <scope>provided</scope>
</dependency>
Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Tiago
  • 691
  • 7
  • 12
  • This environment agnostic answer worked for me, I was on Intellij IDE converting my spring boot app to work with my existing dedicated tomcat server when i got the above `ServletException` error. Thanks bunch – DaddyMoe Apr 07 '15 at 22:31
13

import javax.servlet

STEP 1

Go to properties of your project ( with Alt+Enter or righ-click )

STEP 2

check on Apache Tomcat v7.0 under Targeted Runtime and it works.

source: https://stackoverflow.com/a/9287149

Community
  • 1
  • 1
nalinc
  • 7,375
  • 24
  • 33
4

Add the server(tomcat) from Right click on the Project and select the "Properties" go to "Project Factes" "Runtime tab" other wise "Target Runtime"

if it is maven pom.xml issue, try added this to the pom.xml

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

it will solve the issue.

1
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.2-b02</version>
    <scope>provided</scope>
</dependency>

worked for me.

ivan.ukr
  • 2,853
  • 1
  • 23
  • 41
0

As almost anyone said, adding a runtime service will solve the problem. But if there is no runtime services or there is something like Google App Engine which is not your favorite any how, click New button right down the Targeted Runtimes list and add a new runtime server environment. Then check it and click OK and let the compiler to compile your project again.

Hope it helps ;)

Arash moradabadi
  • 230
  • 1
  • 12