Java Standard Tag Library (JSTL)

Introduction

 
The JSP Standard Tag Library (JSTL) is a new component being offered in Java Server Pages (JSP) programming. The customs tags enable the users to reuse valuable components. However , they need to be created , tested and debugged over and over again. To solve this problem, JSTL was created. It provides a set of reusable standard tags. JSTL is the answer to developers' demands for a way to handle the tasks such as conditional processing internationalization, database access and XML processing.
 
JSTL is a standard set of commonly-used tag libraries. This library has standard set of tags used to write and develop JSP pages. JSTL provides the user with a script- free environment. The user can write code using tags instead of Java codes and Scriptlets.
 
JSTL contains iterator tags, conditional tags, internationalization tags and SQL tags. Iterators and conditional tags are used to control the flow in a JSP page.
 
Internationalization tags provide users with the required language and formatting for localization and internationalization. SQL tags are used in JSTL for accessing databases and updating tables.
 

Types of Tag Libraries

 
JSTL provides four main tag libraries.
 
Core Tag Library
 
This library contains the tags that perform important and basic functions for nearly all the web applications.
 
This includes looping, expressions, evaluation and basic input and output.
 
Internationalization and Formatting Tag Library
 
This library contains the tags that are used to parse the data such as date and time based on the current location.
 
SQL tag Library
 
This Library contains tags thatare used to access SQL databases. It provides an interface for executing SQL queries on database through JSP.
 

XML Tag Library

 
This library contains tags for accessing XML elements. In JSTL, XML processing is an important features, as it is widely used in Web application.
 
Core Tag
 
The core tag is used nearly in all web applications. This tag is useful for performing basic input and output, for looping operations or for evaluating expressions.
 
Various tags used in core library like <c:out>, <c:set>, <c:if>, <c:choose>, <c:when>, <c:otherwise>, <c:forEach>, <c:forTokens>, <c:url>, <c:redirect>, <c:param>, <c:import>
For example
 
<c:out> Tag
  1. <%@page content type=”text/html”%><%@taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core”%>  
  2. <html>  
  3.     <body>  
  4.         <form method =post action=”first.jsp”>  
  5. Name   
  6.             <input type =text name=”text”/value=””>  
  7.                 <br>  
  8.                     <br>  
  9.                         <input type=submit value=”Enter”>  
  10.                         </form>  
  11. Name:   
  12.                         <c:out value=”$(param.text1)” />  
  13.                         <br>  
  14.                         </body>  
  15.                     </html>   
 <c:set> Tag
  1. <%@page content type=”text/html”%><%@taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core”%>  
  2. <html>  
  3.     <body bgcolor=”yellow”>  
  4.         <form method=post action=hello.jsp>  
  5.             <select name=”combo”>  
  6.                 <option=””> Select  
  7.   
  8.                     <option value=”Oracle”>Oracle  
  9.   
  10.                         <option value=”Java”>Java  
  11.   
  12.                             <option value=”Angular JS”>Angular JS  
  13.   
  14.                                 <option value=”Cloud”>Cloud  
  15.   
  16.                                 </select>  
  17.                                 <input type=”submit” value=”Enter”>  
  18.                                 </form>  
  19.                                 < c:set var =”course” value=”$(param.combo)”/>  
  20. My Favorite Language is   
  21.                                 <strong>  
  22.                                     <c:out value=”${course}” />  
  23.                                 </strong> very interesting!!!”  
  24.   
  25.                                 <br>  
  26.                                 </body>  
  27.                             </html>   
<c:if> Tag
  1. <%@page content type=”text/html”%><%@taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core”%>  
  2. <html>  
  3.     <body bgcolor=”yellow”>  
  4.         <form method=post action=hello.jsp>  
  5.             <select name=”combo”>  
  6.                 <option=””> Select  
  7.   
  8.                     <option value=”Oracle”>Oracle  
  9.   
  10.                         <option value=”Java”>Java  
  11.   
  12.                             <option value=”Angular JS”>Angular JS  
  13.   
  14.                                 <option value=”Cloud”>Cloud  
  15.   
  16.                                 </select>  
  17.                                 <input type=”submit” value=”Enter”>  
  18.                                 </form>  
  19.                                 < c:set var =”course” value=”$(param.combo)”/>  
  20.                                 <c:if test= “$(course==’Java’)”>  
  21. Subject   
  22.                                     <strong>  
  23.                                         <c:out value=”${course}” />  
  24.                                     </strong> is interesting…  
  25.   
  26.                                 </c:if>  
  27.                                 <c:if test= “${course==’ Cloud’}”>  
  28. Subject   
  29.                                     <strong>  
  30.                                         <c:out value=”${course}” />  
  31.                                     </strong> is interesting…  
  32.   
  33.                                 </c:if>  
  34.                                 <c:if test= “${course==’ Angular JS }”>  
  35. Subject   
  36.                                     <strong>  
  37.                                         <c:out value=”${course}” />  
  38.                                     </strong> is interesting…  
  39.   
  40.                                 </c:if>  
  41.                                 <br>  
  42.                                 </body>  
  43.                             </html>   
<c:choose>Tag
 
We can only select items using <c:choose> tag. The <c:choose> contains the <c:when>and <c:otherwise> tags for specifying the selection criteria. In the following example, we will see how to make use of these tags. For <c:when> tag test standard variable is being used.
  1. <%@page content type=”text/html”%><%@taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core”%>  
  2. <html>  
  3.     <body bgcolor=”yellow”>  
  4.         <form method=post action=hello.jsp>  
  5.             <select name=”combo”>  
  6.                 <option=””> Select  
  7.   
  8.                     <option value=”Oracle”>Oracle  
  9.   
  10.                         <option value=”Java”>Java  
  11.   
  12.                             <option value=”Angular JS”>Angular JS  
  13.   
  14.                                 <option value=”Cloud”>Cloud  
  15.   
  16.                                 </select>  
  17.                                 <input type=”submit” value=”Enter”>  
  18.                                 </form>  
  19.                                 < c:set var =”course” value=”$(param.combo)”/>  
  20.                                 <c:choose>  
  21.                                     <c:when test=”${course==’Oracle’}”>  
  22. Subject   
  23.                                         <strong>  
  24.                                             <c:out value=”${course}” />  
  25.                                         </strong> is interesting…  
  26.   
  27.                                     </c:when>  
  28.                                     <c:when test=”${course==’Java’}”>  
  29. Subject   
  30.                                         <strong>  
  31.                                             <c:out value=”${course}” />  
  32.                                         </strong> is interesting…  
  33.   
  34.                                     </c:when>  
  35.                                     <c:when test=”${course==’Cloud’}”>  
  36. Subject   
  37.                                         <strong>  
  38.                                             <c:out value=”${course}” />  
  39.                                         </strong> is interesting…  
  40.   
  41.                                     </c:when>  
  42.                                     <c:otherwise>  
  43. Select any subject of your choice  
  44. </c:otherwise>  
  45.                                 </c:choose>  
  46.                                 <br>  
  47.                                 </body>  
  48.                             </html>  
 
<c:forEach>
 
This tag is especially used to denote the looping condition.
  1. <%@page content type=”text/html”%><%@taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core”%>  
  2. <html>  
  3.     <body><%pageContext.setAttribute(“Course”, new String[] {“Oracle”,”Java”, “SQL”,”WebTech”}); %>  
  4.         <b> The Various Courses are </b>  
  5.         <c:forEach var=”I” items=”${course}” varStatus=”n”>  
  6.             <br />  
  7.             <c:out value=”${n.index}”/>  
  8.             <c:out value=”${n.current}”/>  
  9.         </c:forEach>  
  10.     </body>  
  11. </html>   
In this code , we have used <c:forEach> tag in which items denote the current string. var is a symbolic name for array. varStatus is the symbolic name for current status. Using index we can display the index of each item in an array. By default array starts from index 0 and current is used to display the current element.
 
Forloop.jsp
  1. <%@page content type=”text/html”%><%@taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core”%>  
  2. <html>  
  3.     <body>  
  4.         <b> The Natural Numbers are …. </b>  
  5.         <c:forEach var=”i” begin=”1” end=”20”>  
  6.             <br/>  
  7.             <c:out value “${i}” />  
  8.         </c:forEach>  
  9.     </body>  
  10. </html>    
<c:forTokens>
  1. <%@page content type=”text/html”%><%@taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core”%>  
  2. <c:setvar=”i” value=”Ashish;Bhatnagar;Ghaziabad;” />  
  3. <html>  
  4.     <body>  
  5.         <table border=3>  
  6.             <tr>  
  7.                 <th> Name </th>  
  8.                 <th>Last Name</th>  
  9.                 <th>Place</th>  
  10.             </tr>  
  11.             <c:forTokens items=”${i}” delims=”;” var=”tok”>  
  12.                 <td>  
  13.                     <c:outvalue=”${tok}”/>  
  14.                 </td>  
  15.             </c:forTokens>  
  16.         </tr>  
  17.     </table>  
  18. </body>undefined</html>   
In the above code , we have taken a variable i to which some string containing many substrings is assigned. These substrings are separated out of the delimiter. Then using the token <c:forTokens> we tokenize the string using items=”${i}}. We must assign the separator to the delim so that while parsing the given string for separating out the tokens, the delim value is treated as a separation symbol and only token strings can be extracted.
 
<c:url>
 
As the name suggests this tag is used to navigate to the desired page using the URL.
  1. <%@page content type=”text/html”%><%@taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core”%>  
  2. <html>  
  3.     <body>  
  4.         <c:import url=”Mypage.html” />  
  5.         <c:out value =”Thanks for using this Demo….” />  
  6.     </body>  
  7. </html>   
MyPage.html
  1. <html>  
  2.     <body>  
  3.         <h1> The Visitors of this site are always Blessed Persons….!!</h1>  
  4.     </body>  
  5. </html>   
<c:redirect>
 
This tag must slightly change in the above JSP page as follows,
  1. <%@page content type=”text/html”%><%@taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core”%>  
  2. <html>  
  3.     <body>  
  4.         <c:redirect url=”http://localhost:8080/jsp-examples/MyPage.html” />  
  5.         <c:out value=”Thanks for using this Demo…” />  
  6.     </body>  
  7. </html>   
MyPage.html
  1. <html>  
  2.     <body>  
  3.         <h1> The Visitors of this site are always Wonderful…</h1>  
  4.     </body>  
  5. </html>   
<c:param>
 
This tag is useful to send some parameters to another page, we have written one main JSP page on which we have given a redirecting link to the newpage.jsp and while navigating to this page, we have sent a parameter MyName by assigning the same name value to it.
 
paramDemo.jsp
  1. <%@page content type=”text/html”%><%@taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core”%>  
  2. <c:redirect url=”http://localhost:8080/jspexamples/newPage.jsp”>  
  3.     <c:param name=”myname” value=”ashish” />   
 newPage.jsp
  1. <%@taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core”%>  
  2. <html>  
  3.     <body>  
  4.         <strong> Just Look at the address box of your Web Browser</strong>  
  5.         <c:out value=”${param.myname}” />  
  6.     </body>  
  7. </html>    
 <c:import>
 
importDemo.jsp
  1. <%@page content type=”text/html”%><%@taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core”%>  
  2. <html>  
  3.     <body>  
  4.         <c:import url=” http://localhost:8080/jspexamples/myPage.html”%>  
  5.             <c:out value=”Thanks for Using this Demo…” />  
  6.         </body>  
  7.     </html>    
myPage.html
  1. <html>  
  2.     <body>  
  3.         <h1> The Visitors are always Welcome…!!!</h1>  
  4.     </body>  
  5. </html>    

Summary

 
JSTL provides a set of reusable standard tags, JSTL is a standard set of commonly used tag libraries. This library has a standard set of tags used to write and develop JSP pages. The <c:choose> contains the <c:when>and <c:otherwise> tags for specifying the selection criteria. <c:forEach> tag is specially used to denote the looping condition.
 
<c:url> tag is used to navigate to the desired page using the URL. <c:param> tag is useful to send some parameters to another page.


Similar Articles