无法在tomcat中编译JSP类

无法在tomcat中编译JSP类,jsp,tomcat,jsp-tags,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

症状

项目中的任何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) 包含的文件是
check.jspf
。JSPF中的代码如下所示:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <include-prelude>/check.jspf</include-prelude>
    </jsp-property-group>
</jsp-config>
<%@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部署到产品系统中时,会显示错误。我不明白错误消息的意思是什么,这里没有重复的变量!有人知道吗?多谢各位

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

我建议将
check.jspf
替换为一个。只需将相同的代码放入
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);
}
替换为

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

检查过滤器
com.example.CheckFilter
检查过滤器
*.jsp
另见: