问题:org.apache.jasper.JasperException: Unable to compile class for JSP

发布一个从别处拿来的项目后访问登录页面遇到问题:

严重: Servlet.service() for servlet [jsp] in context with path [/train] threw exception [Unable to compile class for JSP: 



An error occurred at line: [54] in the generated java file: [D:\Tomcat8\work\Catalina\localhost\train\org\apache\jsp\Login_jsp.java]
The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory


Stacktrace:] with root cause
org.apache.jasper.JasperException: Unable to compile class for JSP: 


An error occurred at line: [54] in the generated java file: [D:\Tomcat8\work\Catalina\localhost\train\org\apache\jsp\Login_jsp.java]
The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory


Stacktrace:
at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:198)
at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:450)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:361)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:580)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:356)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)

at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)


猜测是否是导入的jar的问题,把build path中的javaEE5 Library 换成了JavaEE6,项目 Clean后再Build,然后发布。问题依旧。

项目中使用的jre是1.7版,而MyEclipse首选项中的Java Compiler是1.6,把它改成1.7,结果项目上出现叉叉。

查看Problem窗口,看到有一句这样的错误信息:

Description Resource Path Location Type
Java compiler level does not match the version of the installed Java project facet. train Unknown Faceted Project Problem (Java Version Mismatch)

再在首选项中MyEclipse->Project Facets 中看到,此处的Java版本是1.6,把它也改为1.7,如下:



做了很多尝试:

去除Build Path中对JavaEE6的导入。

将web.xml中的版本由2.5改为3.0。

换成MyEclipse内置的Tomcat.

...

都不管用.

最后发现WebContent\lib目录下除了servlet-api.jar,还有另一个servlet.jar,这个jar查看后确定是另一个版本的同类文件。删除此servlet.jar,保留和Tomcat\lib下一致的servlet-api.jar,Clean->Build再deployed,终于正常了!

此时我项目中导入的是jre1.7的库,项目的Project Facets是1.6,工作空间的Compiler兼容版本是1.6.

Build Path:


WEB-INF\lib下面的jar:


web.xml中依然是3.0规范。

总结一句:项目中使用的servlet api 的版本一定要和Tomcat下面的一致。

初学JavaEE碰到的问题,求大神帮帮忙 这是源代码 ``` <%@ page contentType="text/html;charset=gb2312" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd"> <html> <head> <title>网上书店</title> <link href="css/bookstore.css" rel="stylesheet" type="text/css"/> </head> <body> <div class="head"> <div class="head_left"> <a href="#"> <img hspace="11" src="picture/logo_dear.gif" vspace="5"> </a> <br>      书店提供专业服务 </div> <div class="head_right"> <div class="head_right_nei"> <div class="head_top"> <div class="head_buy"> <strong> <a href="/bookstore/showCart.jsp"> <img height="15" src="picture/buy01.jpg" width="16"> 购物车 </a> </strong> | <a href="#">用户FAQ</a> </div> </div> <div class="head_middle"> <a class="title01" href="index.jsp"> <span>  首页  </span> </a> <s:if test = "session.user == null"> <a class="title01" href="login.jsp"> <span>  登陆  </span> </a> </s:if> <s:else> <a class="title01" href="logout.action"> <span>  注销  </span> </a> </s:else> <a class="title01" href="register.jsp"> <span>  注册  </span> </a> <a class="title01" href="#"> <span> 联系我们   </span> </a> <a class="title01" href="#"> <span> 网站地图   </span> </a> </div> <div class="head_bottom"> <form action="searchBook.action" method="post"> <input type="text" name="bookname" size="50" align="middle"/> <input type="image" name="submit" src="picture/search02.jpg" align="top" style="width: 48px; height: 22px"/> </form> </div> </div> </div> </div> </body> </html> ``` ![图片说明](https://img-ask.csdn.net/upload/201812/31/1546250296_572812.png)
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页