对于ServletContext类型,方法getContextPath()未定义

时间:2011-09-16 17:53:45

标签: java jsp

这是我的JSP:

<HTML>
<BODY >
path : <%= application.getContextPath() %>
</BODY>
</HTML>

这个文件在Tomcat7和Tomcat6中是正确的,但是当我在我的Cpanel托管的Tomcat 5.32中请求JSP页面时,我得到以下异常:

org.apache.jasper.JasperException: Unable to compile class for JSP: 
An error occurred at line: 3 in the jsp file: /cal2.jsp
The method getContextPath() is undefined for the type ServletContext
1: <HTML>
2: <BODY>
3: path : <%= application.getContextPath() %>
4: </BODY>
5: </HTML>
Stacktrace: 
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:93)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330) 
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:451) 
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:328) 
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:307) 
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:295) 
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:565) 
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:309) 
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308) 
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:72

这个问题是如何引起的?如何解决?

1 个答案:

答案 0 :(得分:11)

在规范2.5版中,getContextPath()方法已添加到ServletContext,但Tomcat 5仅实现了2.4。但是,request.getContextPath()做同样的事情。