Proper Implementation of Loops in JSP: Avoiding Pitfalls of Scriptlet and EL Expression Mixing

Dec 01, 2025 · Programming · 13 views · 7.8

Keywords: JSP loop iteration | JSTL tag library | EL expression scope | scriptlet and EL mixing issues | <c:forEach> best practices

Abstract: This article provides an in-depth exploration of common technical issues when iterating through ArrayList collections in JSP pages, particularly focusing on variable scope conflicts caused by mixing scriptlets with Expression Language (EL). Through analysis of a concrete Festival information display case study, it reveals the root cause: the loop variable i defined in scriptlets cannot be accessed within EL expressions. The paper systematically introduces JSTL (JavaServer Pages Standard Tag Library) as a modern solution, detailing installation and configuration procedures, demonstrating how to replace traditional scriptlet loops with the <c:forEach> tag, and providing complete code refactoring examples. Additionally, it discusses security best practices for disabling scriptlets, XSS protection measures, and proper usage of servlets as MVC controllers.

Problem Analysis: Variable Scope Conflict Between Scriptlets and EL Expressions

In JavaServer Pages (JSP) development, developers frequently need to iterate through collection data and generate dynamic content within pages. The original code example demonstrates a typical issue: attempting to use traditional Java scriptlet loops to traverse an ArrayList containing Festival objects, but when referencing the loop variable in EL expressions, only the element at index 0 is consistently retrieved.

The core problem lies in the different variable scope mechanisms employed by scriptlet tags <% %> and Expression Language ${}. Local variables like i defined within scriptlets are only visible within the scriptlet block, while EL expressions cannot directly access these scriptlet variables. Similarly, allFestivals declared via <jsp:useBean> is available in the EL scope but requires additional handling to access within the scriptlet scope.

Modern Solution: Adopting the JSTL Standard Tag Library

The Java community long recognized the drawbacks of scriptlets in JSP and introduced JSTL as a standardized solution. JSTL provides a set of standardized tags specifically designed for common JSP tasks, including iteration, conditional evaluation, formatting, and more.

JSTL Installation and Configuration

First, JSTL dependencies need to be added to the project. For Maven projects, add to pom.xml:

<dependency>
    <groupId>javax.servlet</groupId>
    <groupId>jstl</groupId>
    <version>1.2</version>
</dependency>

Declare the tag library at the top of the JSP page:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Refactoring Loop Logic with <c:forEach>

The JSTL <c:forEach> tag is specifically designed for collection iteration, completely avoiding the mixing of scriptlets and EL:

<table border="1">
    <tr>
        <td>Festival Name:</td>
        <td>Location:</td>
        <td>Start Date:</td>
        <td>End Date:</td>
        <td>URL:</td>
    </tr>
    <c:forEach items="${allFestivals}" var="festival">
        <tr>
            <td>${festival.festivalName}</td>
            <td>${festival.location}</td>
            <td>${festival.startDate}</td>
            <td>${festival.endDate}</td>
            <td>${festival.URL}</td>
        </tr>
    </c:forEach>
</table>

This implementation offers several advantages:

  1. Unified Scope: The items attribute directly references the allFestivals collection in the EL scope
  2. Variable Binding: The festival variable defined by the var attribute automatically enters the EL scope
  3. Concise Syntax: EL expressions can directly access object properties without explicit getter method calls
  4. Type Safety: JSTL performs type checking at compile time, reducing runtime errors

Security Considerations and Best Practices

XSS Protection Measures

When displaying data provided by users or external sources, cross-site scripting (XSS) attack risks must be considered. JSTL provides the <c:out> tag for output encoding:

<td><c:out value="${festival.festivalName}" /></td>

This converts special characters like <, >, & into HTML entities, preventing malicious script injection.

Security Configuration for Disabling Scriptlets

To ensure development teams don't accidentally use scriptlets, they can be globally disabled in web.xml:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <scripting-invalid>true</scripting-invalid>
    </jsp-property-group>
</jsp-config>

Proper Implementation of MVC Architecture

In modern Java web applications, the MVC pattern should be followed:

Therefore, <jsp:useBean> is generally unnecessary in most cases; data should be set via Servlet:

// In Servlet
List<Festival> festivals = festivalService.getAllFestivals();
request.setAttribute("allFestivals", festivals);
request.getRequestDispatcher("/festivals.jsp").forward(request, response);

Alternative Solutions Comparison

Besides the JSTL solution, other answers mention using scriptlet expressions <%= %>:

<% for(int i = 0; i < allFestivals.size(); i++) { %>
    <tr>
        <td><%= allFestivals.get(i).getFestivalName() %></td>
    </tr>
<% } %>

While this approach solves the variable scope issue, it still suffers from inherent scriptlet drawbacks:

  1. Poor code readability with HTML and Java code intermixed
  2. Difficult maintenance and debugging
  3. Challenging for team collaboration and code review
  4. Non-compliant with modern JSP development best practices

Conclusion

In JSP development, when iterating through collections, priority should be given to using JSTL's <c:forEach> tag, avoiding the mixing of scriptlets and EL expressions. This not only resolves the technical issue of variable scope conflicts but also brings multiple benefits including code maintainability, security, and team collaboration. Combined with configurations to disable scriptlets, XSS protection measures, and proper MVC architecture implementation, robust, secure, and maintainable Java web applications can be built.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.