views:

32

answers:

2

Symptoms:

Any JSP pages in the project produce error messages as below:

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)

Ddevelopment environment:

  • JDK 1.5
  • Tomcat 5.0
  • IDE Eclipse 3.3

Reasons:

I was using jsp-config to include a JSPF in every JSP file. The web.xml is below:

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

The included file is check.jspf. The code in JSPF is below:

<%@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 ");
        }           
    }
%>

The project runs well in Eclipse, but when I deploy the WAR into product system, the error displays. I do not understand what does the error message means, the are no duplicate variables here! Somebody knows? Thank you very much!

A: 

Is it possible that any jsp in which this file gets included declares those variables too ?

kukudas
A: 

I suggest to replace check.jspf by a Filter. Just put the same code in doFilter() method.

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);
}

Replace the <jsp-config> by

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

See also:

BalusC