Eliminating Java Code from JSP: 3 Powerful JSP 2 Approaches

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Goodbye, Spaghetti Code: How to Clean Java Out of JSP Once and For All

\n\n

If you've ever opened an old JSP file and seen a mess of HTML, <% %> scriptlets, and Java logic, you know the feeling. Mixing presentation and business logic turns project maintenance into a nightmare. The question "How can I avoid Java code in JSP files, using JSP 2?" on Stack Overflow has garnered thousands of views — and for good reason.

\n\n

The good news: JSP 2 (Java Server Pages version 2) gave us the tools to forget about scriptlets forever. In this article, I'll show you three real approaches that will turn your JSPs into clean, readable templates. No fluff — just code and practice.

\n\n

Why Are Scriptlets Evil? A Developer's Perspective

\n\n

Scriptlets (those <% ... %> things) violate the main principle of MVC: separation of logic and presentation. Here are three reasons why you should get rid of them:

\n\n
    \n
  • Maintenance complexity. When JSP contains SQL queries and conditional statements, a designer cannot safely edit the layout.
  • \n
  • Difficult testing. Logic inside JSP cannot be tested with unit tests.
  • \n
  • Poor readability. The code turns into "spaghetti" where Java is mixed with HTML.
  • \n
\n\n

JSP 2 offers elegant alternatives. Let's break them down using a real-world task: displaying a list of users from a database.

\n\n

Approach 1: Expression Language (EL) — Your New Best Friend

\n\n

Expression Language (EL) allows you to access Java objects directly in JSP without a single line of Java code. The ${...} syntax is intuitive.

\n\n

Before (with Java code):

\n\n
<% \n    User user = (User) request.getAttribute("user"); \n    out.println("Hello, " + user.getName());\n%>
\n\n

After (with EL):

\n\n
<p>Hello, ${user.name}</p>
\n\n

EL automatically calls getters. If user.getName() returns a string — it will be inserted into the HTML. If the object is null — EL simply outputs nothing (no NullPointerException).

\n\n

Where to use: for outputting data from request, session, application scope, or request parameters.

\n\n

Approach 2: JSTL (JSP Standard Tag Library) — Loops and Conditions Without Java

\n\n

JSTL is a set of ready-made tags that cover 90% of typical tasks: loops, conditions, formatting, URL handling. To use it, simply add the dependency to pom.xml (Maven) or download the JAR.

\n\n

Example: displaying a list of users in a table.

\n\n

Before (scriptlets):

\n\n
<table>\n    <%\n        List<User> users = (List<User>) request.getAttribute("users");\n        for (User u : users) {\n    %>\n        <tr><td><%= u.getName() %></td><td><%= u.getEmail() %></td></tr>\n    <%\n        }\n    %>\n</table>
\n\n

After (JSTL + EL):

\n\n
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>\n<table>\n    <c:forEach var="u" items="${users}">\n        <tr>\n            <td>${u.name}</td>\n            <td>${u.email}</td>\n        </tr>\n    </c:forEach>\n</table>
\n\n

Notice: not a single line of Java! The <c:forEach> tag works like a foreach loop, and ${u.name} is an EL expression. The code has become readable and safe.

\n\n

Other useful JSTL tags:

\n
    \n
  • <c:if test="${condition}"> — conditional output
  • \n
  • <c:choose> — switch equivalent
  • \n
  • <c:set var="x" value="..." /> — variable creation
  • \n
  • <fmt:formatDate> — date formatting
  • \n
\n\n

Approach 3: Custom Tags (User-Defined

Blogs

Book Recommendations