JSP doesn’t compile
Today I want to examine the JSP most common exceptions which are not obvious for a newcomer and even for an experienced JEE developer. This article requires base knowledge of web-development with Java, Tomcat or any other servlet container and any popular IDE (e.g. Eclipse).
You can develop a Dynamic Web Project or a Spring MVC project and this probably means that you will need to use some technology in order to represent data to an end user. Likely it’s JSP. I suppose that you are already familiar with JEE development and you know the main purpose of JSP and its lifecycle. Further we will examine the main exceptions and difficulties which can occur while you are working with JSP.
- javax.servlet.jsp.PageContext cannot be resolved to a type
- JspFactory cannot be resolved
- JspWriter cannot be resolved to a variable
If one of these three unpleasant messages appears in your JSP code – this is a signal that you have a mess with libraries in your project. Seems you haven’t javax.servlet.jsp-api in your build path. The most obvious way to fix this problem is to add it to your build pass. You can do it by adding dependency to your maven pom.xml file or adding library strictly. But don’t forget that Tomcat already contains this library by default (tomcat/lib/).
So in Eclipse you can follow these steps to fix this problem: Open project Properties -> Targeted Runtimes -> select checkbox corresponding to your server. This should be enough to fix the issue.
Second problem is very close to the first:
- The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory
- JasperException: Unable to compile class for JSP
These two messages can be displayed after you have added some libraries related to JSP or JSTL to your build path. In order to fix this – just remove libraries which are provided by Tomcat (or the server you use) from the build path.