in JSTL

The JSP Standard Tag Library (JSTL) core library, provides custom tags for basic functionality. Instead of using those hardcoded scriptlet, It allows you to program your pages using tags.

in JSTL

<c:forEach> in JSTL

        

The JSP Standard Tag Library (JSTL) core library, provides custom tags for basic functionality. Instead of using those hardcoded scriptlet, It allows you to program your pages using tags. While using the scriptlet, as the program grows bigger it becomes difficult to understand the program, and later in case if any programmer wants to change any module or part of  a code, then it becomes very difficult in understanding the program. Here comes the use of the JSTL, which allows you to program the pages using tags. Now if a programmer wants any change in the code, he can change it in the tag itself.  JSTL can do nearly everything that the JSP scriptlet can do.

<c:forEach> tag is a simple way to iterate over arrays and collections. This tag is very much similar to for loop. The tag repeats the body of the tag for each element in the array or collection.

To make a program on JSTL we need to use a taglib directory. Make sure to import the jstl.jar and standard.jar in the lib folder inside the WEB-INF folder.

The code of the program is given below:

<%@ taglib prefix = "c" uri = "http://java.sun.com/jstl/core"%>
<% String[] name = {"Hello", "you", "are", "using", "jstl", "in", "jsp"};
request.setAttribute("name", name);
%>
<html>
<body bgcolor="#FFFFCC">
<table align="Center" border="0" cellpadding="0"
  cellspacing="0" width="98%">
<c:forEach var = "itemName" items = "${name}">
<tr>
<font color="#000080"><td>${itemName}</td></font>
</tr>
</c:forEach>
</table>
</body>
</html>

Output of the program is given below:

Download this example.