This is an automated email from the ASF dual-hosted git repository.
markt-asf pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/main by this push:
new adc24f0623 Follow on to PR #1158
adc24f0623 is described below
commit adc24f0623502ea8a57be5a06d2d2047106a1e6a
Author: Mark Thomas <[email protected]>
AuthorDate: Wed Nov 18 12:01:54 2026 +0000
Follow on to PR #1158
Co-authored by: Claude / Sonnet 5.5
---
.../jasper/tagplugins/jstl/core/ForEach.java | 22 ++++-------------
.../jasper/tagplugins/jstl/core/ForTokens.java | 8 ++-----
.../tagplugins/jstl/core/LoopAttributeChecks.java | 28 +++++++++++++++++++++-
webapps/docs/changelog.xml | 6 ++++-
4 files changed, 38 insertions(+), 26 deletions(-)
diff --git a/java/org/apache/jasper/tagplugins/jstl/core/ForEach.java
b/java/org/apache/jasper/tagplugins/jstl/core/ForEach.java
index 23f5bffd92..e94fabe8d2 100644
--- a/java/org/apache/jasper/tagplugins/jstl/core/ForEach.java
+++ b/java/org/apache/jasper/tagplugins/jstl/core/ForEach.java
@@ -96,7 +96,7 @@ public final class ForEach implements TagPlugin {
}
if (hasVar) {
- ctxt.generateJavaSource("try {");
+ LoopAttributeChecks.generateTryStart(ctxt);
}
ctxt.generateJavaSource("for (int " + index + " = " + beginName + "; "
+ index + " <= " + endName + ";");
if (hasStep) {
@@ -115,7 +115,7 @@ public final class ForEach implements TagPlugin {
ctxt.generateBody();
ctxt.generateJavaSource("}");
if (hasVar) {
- generateRemoveVar(ctxt);
+ LoopAttributeChecks.generateRemoveVar(ctxt);
}
}
@@ -159,7 +159,7 @@ public final class ForEach implements TagPlugin {
}
if (hasVar) {
- ctxt.generateJavaSource("try {");
+ LoopAttributeChecks.generateTryStart(ctxt);
}
// An empty range must not execute the body or consume any items.
if (hasBegin && hasEnd) {
@@ -260,24 +260,10 @@ public final class ForEach implements TagPlugin {
ctxt.generateJavaSource("}"); // Non-empty range
}
if (hasVar) {
- generateRemoveVar(ctxt);
+ LoopAttributeChecks.generateRemoveVar(ctxt);
}
}
-
- /**
- * Close the {@code try} block that surrounds the loop and generate the
code that removes the loop variable. The
- * variable has nested visibility so, as with the standard tag handler, it
is removed from the page scope when the
- * loop ends, including when the body throws an exception.
- */
- private void generateRemoveVar(TagPluginContext ctxt) {
- ctxt.generateJavaSource("} finally {");
- ctxt.generateJavaSource("_jspx_page_context.removeAttribute(");
- ctxt.generateAttribute("var");
- ctxt.generateJavaSource(",
jakarta.servlet.jsp.PageContext.PAGE_SCOPE);");
- ctxt.generateJavaSource("}");
- }
-
/**
* Generate iterators for data types supported in items
*/
diff --git a/java/org/apache/jasper/tagplugins/jstl/core/ForTokens.java
b/java/org/apache/jasper/tagplugins/jstl/core/ForTokens.java
index 7b566ec2f5..5018723057 100644
--- a/java/org/apache/jasper/tagplugins/jstl/core/ForTokens.java
+++ b/java/org/apache/jasper/tagplugins/jstl/core/ForTokens.java
@@ -114,7 +114,7 @@ public class ForTokens implements TagPlugin {
// The loop. The variable has nested visibility so it is removed from
the page scope when the loop ends, including
// when the body throws an exception, as with the standard tag handler.
if (hasVar) {
- ctxt.generateJavaSource("try {");
+ LoopAttributeChecks.generateTryStart(ctxt);
}
ctxt.generateJavaSource(
"for(int " + index + " = " + beginName + "; " + index + " <= "
+ endName + "; " + index + "++){");
@@ -129,11 +129,7 @@ public class ForTokens implements TagPlugin {
ctxt.generateJavaSource(" }");
ctxt.generateJavaSource("}");
if (hasVar) {
- String strVar = ctxt.getConstantAttribute("var");
- ctxt.generateJavaSource("} finally {");
- ctxt.generateJavaSource("
_jspx_page_context.removeAttribute(\"" + strVar +
- "\", jakarta.servlet.jsp.PageContext.PAGE_SCOPE);");
- ctxt.generateJavaSource("}");
+ LoopAttributeChecks.generateRemoveVar(ctxt);
}
}
diff --git
a/java/org/apache/jasper/tagplugins/jstl/core/LoopAttributeChecks.java
b/java/org/apache/jasper/tagplugins/jstl/core/LoopAttributeChecks.java
index c358152007..b7c2ce4bb5 100644
--- a/java/org/apache/jasper/tagplugins/jstl/core/LoopAttributeChecks.java
+++ b/java/org/apache/jasper/tagplugins/jstl/core/LoopAttributeChecks.java
@@ -21,7 +21,8 @@ import org.apache.jasper.compiler.tagplugin.TagPluginContext;
/**
* Generates the validation of the <code>begin</code>, <code>end</code> and
<code>step</code> loop control attributes
* shared by the <c:forEach> and <c:forTokens> tag plugins. The
checks and messages match the setters of the
- * reference implementation, <code>LoopTagSupport</code>.
+ * reference implementation, <code>LoopTagSupport</code>. It also generates
the code that removes the loop variable from
+ * the page scope once the loop has ended.
*/
final class LoopAttributeChecks {
@@ -61,6 +62,31 @@ final class LoopAttributeChecks {
generateCheck(ctxt, variable, "<=", "'step' <= 0");
}
+ /**
+ * Generates the start of a <code>try</code> block that must surround the
loop so that the loop variable is removed
+ * when the loop ends. Must be paired with a call to {@link
#generateRemoveVar(TagPluginContext)} after the loop.
+ *
+ * @param ctxt The tag plugin context
+ */
+ static void generateTryStart(TagPluginContext ctxt) {
+ ctxt.generateJavaSource("try {");
+ }
+
+ /**
+ * Closes the <code>try</code> block opened by {@link
#generateTryStart(TagPluginContext)} and generates the code
+ * that removes the <code>var</code> attribute from the page scope. The
variable has nested visibility so, as with
+ * the standard tag handlers, it is removed when the loop ends, including
when the body throws an exception.
+ *
+ * @param ctxt The tag plugin context
+ */
+ static void generateRemoveVar(TagPluginContext ctxt) {
+ ctxt.generateJavaSource("} finally {");
+ ctxt.generateJavaSource("_jspx_page_context.removeAttribute(");
+ ctxt.generateAttribute("var");
+ ctxt.generateJavaSource(",
jakarta.servlet.jsp.PageContext.PAGE_SCOPE);");
+ ctxt.generateJavaSource("}");
+ }
+
private static void generateCheck(TagPluginContext ctxt, String variable,
String operator, String message) {
ctxt.generateJavaSource("if (" + variable + " " + operator + " 0) {" +
"throw new jakarta.servlet.jsp.JspTagException(\"" + message +
"\");}");
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 2f1fcc50bc..c4c8fd17c3 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -395,6 +395,10 @@
<bug>70241</bug>: Cache the public methods used to resolve EL method
calls. Pull request <pr>1155</pr> provided by Arturo Bernal. (markt)
</fix>
+ <fix>
+ Remove the loop variable after the ForEach and ForTokens tag plugins.
+ Pull request <pr>1158</pr> provided by Arturo Bernal. (markt)
+ </fix>
</changelog>
</subsection>
<subsection name="Cluster">
@@ -459,7 +463,7 @@
<docs>
Add documentation for multicast, static and cloud membership in Apache
Tribes, including failure detection, security and troubleshooting
- guidance. (sainadh777)
+ guidance. Based on pull request <pr>153</pr> by sainadh777. (markt)
</docs>
</changelog>
</subsection>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]