Project: Tomcat
Bug 52776 Generator's JspFragment.invoke implementation does not reset ELContext[JspContext.class]
Reported: 2012-02-27 00:46:22 | Status: resolved fixed

If a developer captures a JspFragment themselves and calls invoke on it, the JspContext.class attribute in the enclosing pageContext's ELContext is set by the generated JspFragment subclass, but not reset to its original value afterwards. Therefore after invoking a JspContext any EL expressions will be searching the wrong jspContext for attribute values. org.apache.jasper.compiler.Generator is responsible for creating the JspFragment subclass to represent the compiled fragment. The generatePostamble() method emits generated code to set the JspContext attribute in the ELContext: out.printil("this.jspContext.getELContext().putContext(javax.servlet.jsp.JspContext.class,this.jspContext);"); However it doesn't later put back the original value. If you are using the <jsp:invoke> tag to invoke the fragment (which is undoubtedly what most people are doing) then the code generated by visit(Node.InvokeAction n) in Generator explicitly restores the EL context: out.printil("jspContext.getELContext().putContext(javax.servlet.jsp.JspContext.class,getJspContext());"); However if you call invoke(Writer) on the JspFragment yourself you don't get that tidy up code. I suggest the resolution is to have the JspFragment.invoke method tidy up after itself. It "puts" the change to the JspContext.class attribute so it should also re"put" it. Then there shouldn't be any need to restore the EL context in the generated <jsp:invoke> code. I have tested this solution successfully.

Files changed in fix:
java/org/apache/jasper/compiler/Generator.java