13

This is a follow up to a question on accessing resources in jsp page of spring mvc app Thanks to @kmb385 I was able to resolve that problem but now I get the following eclipse errors in my JSP file javax.servlet.jsp.JspException cannot be resolved to a type and

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

as suggest by kmb385, here is my controller:

@Controller
public class AdminController {

        @RequestMapping("/")
        protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

            ModelAndView model = new ModelAndView("index");
            model.addObject("msg", "hello world");

            return model;
        }   
    }

and here is my index.jsp page just in case:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<!-- <link type="text/css" rel="stylesheet" href="css/style.css"/> -->
<style type="text/css">
    <%@include file="css/style.css" %>
    </style>
<title>My Project - MainPage</title>
</head>
<body>
<h2 class="main_heading2">My Project - MainPage</h2>
<div style="float: right; width: 30%; text-align: center">
<p style="float:left;">an image should be here</p>
<img src="images/logo.jpg" alt="Logo"/>
<img src="${pageContext.servletContext.contextPath}/resources/images/logo.jpg" />
</div>

</body>

I have come across "solutions" to this by disabling it in the JSP validator but Please do not suggest this unless you can give a legitimate reason. I would rather correct this issue properly

Any Help appreciated

UPDATE: Build path screen grab as requested by @kmb385 Eclipse Build Path

Community
  • 1
  • 1
jonnie
  • 12,260
  • 16
  • 54
  • 91
  • Should the error say pageContext cannot be resolved to a type instead of content? Also, I assume your using Tomcat? Checkout, http://stackoverflow.com/questions/8669387/javax-servlet-jsp-pagecontext-cannot-be-resolved-to-a-type – Kevin Bowersox Nov 14 '12 at 10:56
  • @kmb385 Sorry yes you are correct, I will edit that – jonnie Nov 14 '12 at 11:01

7 Answers7

36

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.

Lonzak
  • 9,334
  • 5
  • 57
  • 88
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • I've added that (I updated Maven, refreshed project and cleaned project) but it seems to have no effect – jonnie Nov 14 '12 at 11:29
  • @jonnieM I noticed another difference between our pom.xml files. – Kevin Bowersox Nov 14 '12 at 11:39
  • thats seems to remove the `javax.servlet.jsp.JspException cannot be resolved to a type` but I now get `javax.servlet.jsp.JspWriter cannot be resolved to a type` , `JspWriter cannot be resolved to a variable` & `JspFactory cannot be resolved to a type` as well as the `javax.servlet.jsp.PageContext cannot be resolved to a type` – jonnie Nov 14 '12 at 11:54
  • What app server are you using? Tomcat provides these libraries but I am uncertain if other app servers do. Try removing provided from the xml for each dependency. http://stackoverflow.com/questions/7001936/java-project-problem-with-jspwriter – Kevin Bowersox Nov 14 '12 at 12:00
  • I tried removing the provided scope and still no effect - tomcat 6 is the app server I'm using – jonnie Nov 14 '12 at 12:08
  • @jonnieM just realized you were discussing eclipse errors, I updated my answer. – Kevin Bowersox Nov 14 '12 at 12:19
  • I updated the maven project config every time I added the dependencies, still didn't have any effect – jonnie Nov 14 '12 at 13:25
  • These is an issue on your build path, could you post an image of it in eclipse? – Kevin Bowersox Nov 14 '12 at 13:35
  • do you want a screen grab of the java build path in properties in eclipse for the project? all I have in it are `JRE system Library[JavaSE-1.6]` and `Maven Dependencies` but I will add a screen grab to the question, Thanks again for your help so far – jonnie Nov 14 '12 at 13:51
  • Everything looks ok to me. Your issue is due to a missing import on the build path and its one of the two jars I mentioned. Maybe try exporting those jars on the export and order tab? – Kevin Bowersox Nov 15 '12 at 01:12
  • @Kbm385 I deleted those jars from the build path and updated the Maven project again and that seemed to solve it. Thank you for your help – jonnie Nov 15 '12 at 09:02
  • @jonnieM I'm very glad to hear you got this resolved, it was bothering me. I was about to login an offer to look at your project if you would send it to me. Glad there was no need. Way to figure it out! – Kevin Bowersox Nov 15 '12 at 09:58
6

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.

bpjoshi
  • 1,091
  • 16
  • 23
3

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"%>

mwangi
  • 1,616
  • 1
  • 20
  • 23
  • I get an error `The import javax.servlet.jsp.PageContent cannot be resolved` on top of the previous errors when I use this – jonnie Nov 14 '12 at 11:32
  • ok. Put javax.servlet.jsp.jar in your classpath. Download from here http://www.java2s.com/Code/JarDownload/javax/javax.servlet.jsp.jar.zip – mwangi Nov 14 '12 at 12:35
  • I did have that jar in my maven dependencies but was not registering correctly, I deleted it from the build path and updated the Maven project again and that seemed to solve it. Thank you for your input – jonnie Nov 15 '12 at 09:04
  • It's PageContext, not content :) – kboom Jul 10 '14 at 21:11
1

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

Community
  • 1
  • 1
mfruizs
  • 770
  • 14
  • 20
0

How to solve javax.servlet.jsp.PageContext cannot be resolved to a type

1:- Select your project and Right click

2:- Go to Properties

3:- Click Targated Runtimes

4:- Check mark "Apache Tomcat v8.0"

I am using Apache v8.0 in my case

Sachindra N. Pandey
  • 1,177
  • 17
  • 15
0
This will solve the problem 

<!-- Need this to compile JSP -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
0

This alternative worked for me <%=request.getContextPath()%> which gets the Application context.

Wes
  • 1,847
  • 1
  • 16
  • 30