无法在tomcat中编译JSP的类

时间:2010-10-09 09:07:20

标签: jsp tomcat jsp-tags

症状

项目中的任何JSP页面都会产生如下错误消息:

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

An error occurred at line: 5 in the jsp file: /check.jspf
Generated servlet error:
*Duplicate local variable usr*

An error occurred at line: 5 in the jsp file: /check.jspf
Generated servlet error:
*Duplicate local variable path*

An error occurred at line: 5 in the jsp file: /check.jspf
Generated servlet error:
*Duplicate local variable pos*

An error occurred at line: 5 in the jsp file: /check.jspf
Generated servlet error:
*Duplicate local variable hSession*

An error occurred at line: 5 in the jsp file: /check.jspf
Generated servlet error:
*Duplicate local variable testRegister*

org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:512)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    com.byinsight.logic.EncodingFilter.doFilter(EncodingFilter.java:22)

重建环境

  • JDK 1.5
  • Tomcat 5.0
  • IDE Eclipse 3.3

原因

我使用jsp-config在每个JSP文件中包含JSPF。 web.xml位于以下位置:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <include-prelude>/check.jspf</include-prelude>
    </jsp-property-group>
</jsp-config>

包含的文件是check.jspf。 JSPF中的代码如下:

<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="com.byinsight.model.User"%>
<%@page import="javax.servlet.http.HttpSession"%>

<%
    //String str = (String)session.getAttribute("login");
    User usr = (User)session.getAttribute("user");
    String path = request.getServletPath();
    int pos = path.indexOf("index.jsp");
    HttpSession hSession = request.getSession(false);
    String testRegister = (String)hSession.getAttribute("register");
    if (-1 == pos) {
        if ((null == usr) && (!testRegister.equals("login"))) {
            throw new RuntimeException("you have to login first ");
        }           
    }
%>

该项目在Eclipse中运行良好,但是当我将WAR部署到产品系统时,会显示错误。我不明白错误信息的含义是什么,这里没有重复的变量!有人知道吗?非常感谢你!

2 个答案:

答案 0 :(得分:0)

是否有可能包含此文件的任何jsp声明这些变量?

答案 1 :(得分:0)

我建议将check.jspf替换为Filter。只需在doFilter()方法中添加相同的代码即可。

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
    HttpServletRequest req = (HttpServletRequest) request;
    HttpSession session = req.getSession();
    User usr = (User) session.getAttribute("user");
    String path = req.getServletPath();
    int pos = path.indexOf("index.jsp");
    String testRegister = (String) session.getAttribute("register");
    if (-1 == pos) {
        if ((null == usr) && (!testRegister.equals("login"))) {
            throw new RuntimeException("you have to login first "); // I'd just redirect to login page rather than throwing a RuntimeException.
        }           
    }
    chain.doFilter(request, response);
}

<jsp-config>替换为

<filter>
    <filter-name>checkFilter</filter-name>
    <filter-class>com.example.CheckFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>checkFilter</filter-name>
    <url-pattern>*.jsp</url-pattern>
</filter-mapping>

另见: