Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JspException and PageContext cannot be resolved

Tags:

spring

jsp

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

like image 327
jonnie Avatar asked Nov 14 '12 10:11

jonnie


3 Answers

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.

like image 88
Kevin Bowersox Avatar answered Nov 17 '22 01:11

Kevin Bowersox


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.

like image 6
bpjoshi Avatar answered Nov 17 '22 02:11

bpjoshi


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

like image 3
mwangi Avatar answered Nov 17 '22 00:11

mwangi