Home > Articles > Programming > Java

Comparing JSTL and JSP Scriptlet Programming

📄 Contents

  1. How Is JSTL Different from Scriptlet JSP?
  2. Advantages of JSTL
  3. Disadvantages of JSTL
  4. Conclusions
  • Print
  • + Share This
Sun's new method of programming JSP, JSP Tag Library (JSTL), allows you to program JSP pages using tags rather than scriptlet Java code. Jeff Heaton discusses how it can provide a more consistent source code that is easier for HTML programmers to learn.
From the author of

If you are involved with Java Web development and work with JSP pages, you may have heard of a new technology called JSTL. JSTL is an acronym that stands for JSP Standard Tag Library. JSTL allows you to program JSP pages using tags rather than the scriptlet code that has been used with JSP up to this point. This article explains the differences between scriptlet JSP code and JSTL code. The article concludes by explaining what you should consider to make the decision between scriptlet JSP and the new JSTL.

How Is JSTL Different from Scriptlet JSP?

To understand the difference between JSTL and scriptlet-based JSP, you must understand what exactly is meant by scriptlet-based JSP. When most programmers refer to JSP programming what they are most likely talking about is scriptlet-based JSP programming. An example of scriptlet-based programming, which counts to 10, is shown here:

<html>
 <head>
  <title>Count to 10 in JSP scriptlet</title>
 </head>
 <body>
<%
 for(int i=1;i<=10;i++)
{%>
<%=i%><br/>
<%
}
%>
 </body>
</html>

As you can see from the preceding example, using scriptlet code produces page source code that contains a mix of HTML tags and Java statements. There are several reasons why this mixing of programming styles is not optimal.

The primary reason why you shouldn't mix scriptlet and tag-based code is the readability issue, which applies both to humans and computers. JSTL allows the human programmer to look at a program that consists entirely of HTML and HTML-like tags.

Additionally, scriptlet code can be more difficult for a HTML programmer to produce. A HTML programmer must learn the syntax of Java to produce scriptlet code. With JSTL, the HTML programmer is already programming in a familiar tag-based syntax.

Now, I will show you how JSTL alleviates this mixing of code. Consider the following example, which shows how to count from 1 to 10 using JSTL rather than scriptlet code.

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
 <head>
  <title>Count to 10 Example (using JSTL)</title>
 </head>

 <body>
  <c:forEach var="i" begin="1" end="10" step="1">
   <c:out value="${i}" />

   <br />
  </c:forEach>
 </body>
</html>

When you examine the preceding source code, you can see that the JSP page consists entirely of tags. The code makes use of HTML tags such <head> and <br>. The use of tags is not confined just to HTML tags; this code also makes use of JSTL tags such as <c:forEach> and <c:out>.

  • + Share This
  • 🔖 Save To Your Account