JspException and PageContext cannot be resolved

Add to the pom.xml dependencies:

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

JSP:

Make sure to add on jsp, before tag:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

Get the context on JSP:

<c:set var="contextPath" value="${pageContext.request.contextPath}"/>

Import style css:

<link type="text/css" rel="stylesheet" href="${contextPath}/css/yourCssFile.css"/>

Dispatcher-servlet:

On your "spring-dispatcher-servlet.xml" add the following lines:

<beans xmlns="... xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="...
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<mvc:resources mapping="/resources/**" location="/resources/css/" />

Maybe, you need to add this adapters:

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> [Optional]
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="order" value="0"/>
</bean>  

explained here: how to include js and css in jsp with spring MVC


Try setting the servlet-api dependency in your pom.xml to provided. This jar maybe conflicting with the tomcat provided servlet-api.jar.

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

Also be sure to include the jsp-api dependency, once again set as provided:

    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.3</version>
        <scope>provided</scope>
    </dependency>

Make sure all of your maven dependencies are being used to build the project by right clicking your project > Properties. On the deployment assembly tab click the add button, then Java Build Path Entries, then Maven Dependencies and finally Finish.

You may also need to add your maven dependencies to the build path. Right click your project > Maven > Update Project Configuration.


Try import the class.

Modify your jsp's first line to look like this;

<%@ page language="java" import="javax.servlet.jsp.PageContext" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>


If you have downloaded all the dependencies in maven and the error is still not getting away, follow the steps below:

  1. Right click on project and go to properties
  2. Click over target run time
  3. Check the box in front of the server you are using.

This should work.

Tags:

Spring

Jsp