Lecture Notes: JSTL vs JSP Scriptlets
1. Introduction
JSP (JavaServer Pages) allows embedding Java code in HTML. Traditionally, this was done
using Scriptlets (<% %>). However, this mixes Java code and UI, making maintenance
harder. JSTL (JSP Standard Tag Library) provides a cleaner way using XML-like tags.
2. What is JSTL?
JSTL is a set of standard tags for common tasks like loops, conditions, formatting, and
database operations. It uses Expression Language (EL) with ${} syntax.
3. What are JSP Scriptlets?
Scriptlets are Java code blocks inside JSP using <% %>. Example:
<%
String name = "EXO";
out.println("Hello " + name);
%>
4. JSTL vs Scriptlets Comparison
Feature Scriptlet (<% %>) JSTL
Syntax Java code directly XML-like tags
Readability Harder (logic + UI mixed) Cleaner, separated logic
Maintenance Not recommended for big Preferred in enterprise apps
projects
If condition <% if(user != null){ %> <c:if test="${not empty
user}">
Loop <% for(String n : list){ %> <c:forEach var="n"
items="${list}">
5. Example Codes
a) Scriptlet Example
<%
String[] exo = {"D.O.", "Baekhyun", "Chanyeol"};
for(String member : exo){
out.println("<p>Member: " + member + "</p>");
}
%>
b) JSTL Example
<%
String[] exo = {"D.O.", "Baekhyun", "Chanyeol"};
request.setAttribute("exoList", exo);
%>
<c:forEach var="member" items="${exoList}">
<p>Member: ${member}</p>
</c:forEach>
6. Practice Exercises
Step 1: Display a variable using Scriptlet and JSTL.
Step 2: Use if condition (age check) in both Scriptlet and JSTL.
Step 3: Loop through an array of EXO members using both.
Step 4: Use <c:choose> (like switch-case) with roles admin/user/guest.
Step 5: Summary Task - Create exoMembers.jsp combining all.