48

I am seeing the below errors in my jsp page -

javax.servlet.jsp.PageContext cannot be resolved to a type
javax.servlet.jsp.JspException cannot be resolved to a type

I have seen a post on this and tried few things that were suggested. BalusC provided a great input - JSTL1.2 and Standard.jar must not be used together. I did this and it fixed the issue for sometime - but it is reappearing. I am not sure if I have any more jar collisions. I have defined all the jars as dependencies in Maven. The below are the dependencies that I have specified pom.xml -

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.2.1</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>com.jcraft</groupId>
        <artifactId>jsch</artifactId>
        <version>0.1.38</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.15</version>
        <exclusions>
            <exclusion>
                <groupId>javax.jms</groupId>
                <artifactId>jms</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jmx</groupId>
                <artifactId>jmxri</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jdmk</groupId>
                <artifactId>jmxtools</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>commons-configuration</groupId>
        <artifactId>commons-configuration</artifactId>
        <version>1.6</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-jsp</artifactId>
        <version>2.2.2</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.5.6</version>
    </dependency>

</dependencies>
skaffman
  • 398,947
  • 96
  • 818
  • 769
Punter Vicky
  • 15,954
  • 56
  • 188
  • 315
  • I am unable to answer my question now. The issue is now solved. Instead of JSTL1.2 jar as dependency, I added JSTL-API and JSTL-IMPL jars as specified in this link http://www.andygibson.net/blog/quickbyte/jstl-missing-from-maven-repositories – Punter Vicky Dec 29 '11 at 16:01
  • 12
    In case someone gets this error in eclipse, check project properties -> Project Facets -> Dynamic Web Module -> Runtimes – koppor Dec 08 '12 at 16:20

4 Answers4

73

You will need to import in your project the JSP APIs, which are not included in servlet-api

In my project, the solution is:

<dependency>
  <groupId>javax.servlet.jsp</groupId>
  <artifactId>jsp-api</artifactId>
  <version>2.1</version>
  <scope>provided</scope>
</dependency>
Lonzak
  • 9,334
  • 5
  • 57
  • 88
Oliver
  • 1,360
  • 9
  • 14
  • http://stackoverflow.com/questions/7064269/the-method-getjspapplicationcontextservletcontext-is-undefined-for-the-type-js Seems like your solution raises a different error .[ The accepted answer ] . – Roshan Khandelwal May 06 '14 at 19:23
32

The solution that worked for me, is given in this answer. Go to project properties > Targeted runtimes > Select the checkbox for a runtime (Apache Tomcat 7 in my case).
That's all. Just build the project now and everything will be fine.

Community
  • 1
  • 1
Nav
  • 19,885
  • 27
  • 92
  • 135
  • 1
    Works for me too on Eclipse/Luna. I already had the servlet-api included which deals with the Java dependencies, just needed tell Eclipse which Runtime to check against and all those annoying JSP "errors" disappeared! – Jeff Dec 07 '14 at 23:23
  • 1
    This solution works for me. – Nidheesh Feb 10 '16 at 08:56
  • It works for Marse 1.1 as well – Sachindra N. Pandey Jun 27 '16 at 15:01
  • 1
    This worked for on Mac OS X Eclipse and all these errors disappeared – giri-jeedigunta Jul 24 '16 at 11:40
  • Worked like a charm! Can you explain why eclipse has this behavior? I think, there are some dependencies that we don't put in the POM, but servlet container (Tomcat in this case) provides them instead, and we can put these in our pom under as provided. I'm i right or wrong? i'm just started to learn java – Andres Camilo Sierra Hormiga Jun 05 '22 at 21:52
5

Assuming this is the pom for a web application...

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
</dependency>

A number of these dependencies should be set as provided as they are provisioned by the container. You should not bundle these with your application. See Maven dependency scopes. Failure to do this may result in undefined behaviour.

Exactly which dependencies are provided depends on the container.

McDowell
  • 107,573
  • 31
  • 204
  • 267
0

"The solution that worked for me, is given in this answer. Go to project properties > Targeted runtimes > Select the checkbox for a runtime (Apache Tomcat 7 in my case). That's all. Just build the project now and everything will be fine."

This solution worked for me.

Monika
  • 29
  • 4