Project: Tomcat
Bug 45691 Jasper creates duplicate variable names
Reported: 2008-08-26 07:23:33 | Status: resolved fixed

Sometimes a JSP's Java code, which have been generated by Jasper, has duplicate variable names. Therefore the JSP cannot be compiled and tomcat delivers an HTTP status code 500. You get an error message like this org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 10 in the jsp file: /test1.jsp Duplicate local variable _jspx_temp0 7: <c:set var="a"> 8: <jsp:attribute name="value"><%= new java.util.Date().toString() %></jsp:attribute> 9: </c:set> 10: <c:set var="b"> 11: <jsp:attribute name="value"><%= new java.util.Date().toString() %></jsp:attribute> 12: </c:set> 13: <h1>Test 1</h1> The reason of this error is the generation of variable names like _jspx_temp0, which are created for <jsp:attribute> tags. At the beginning of parsing a JSP file a counter is reset to 0. Every time an <jsp:attribute> tag occurs, the counter is used to build a variable name _jspx_temp<counter>. Thereafter the counter will be incremented. Unfortunately the counter is a static member of the JspUtil class. Suppose there's a thread 1, which created _jspx_temp4. The counter is 5. Now a second thread (thread 2) starts and resets the counter. Afterwards thread 2 creates _jspx_temp0 to _jspx_temp3. The counter is 4 by now. Then thread 2 stops and thread 1 continues. The next variable's name in thread 1 will be _jspx_temp4, which already exists. Therefore the generated java class cannot be compiled.

Files changed in fix:
java/org/apache/jasper/compiler/Compiler.java java/org/apache/jasper/compiler/JspUtil.java java/org/apache/jasper/compiler/Node.java java/org/apache/jasper/compiler/TagPluginManager.java