Java Standard Tag Library (JSTL) in JSP

Introduction to JSTL

  • JSTL is the collection of standard library of custom tags.
  • JSTL provides the set of tags that simplifies the JSP development.
  • Java EE API provides the JSTL which is included in most of the Servlet container.
  • JSTL tags can be used on various pages and avoid the use of scriptlet tag.

JSTL Tags

TagsDescription
core tagProvides URL management, flow control, variable support etc. The prefix of the core tag is 'c'.
sql tagIt provides the SQL support in JSP application. The prefix of sql tag is 'sql'.
XML tagProvides the flow control, transformation etc. the prefix is 'x'.
function tagUsed to provide support for string manipulation and String length. The prefix is 'fn'.

JSTL Core Tags

JSTL core provides the several core tags which are frequently used.

Syntax:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Followings are the JSTL Core tag:

i) c:catch Tag

It is used to handle the exception and doesn’t forward the page to the error page. The exception object are thrown at runtime and stored in variable.

Example : illustrating the c:catch tag

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
     <head>
         <title>Core Tag Example</title>
     </head>
<body>
     <c:catch>
        <%
             int result = 50/0;
        %>
     </ c:catch>
</body>
</html>


ii) c:if Tag

It is used to just evaluate conditional expression. It displays its body content if the expression is true.

Example : illustrating the c:if tag

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
<html>
     <head>
           <title><c:if> Core tag</title>
     </head>
<body>
     <c:set var="number" scope="session" value="${700}">  
     <c:if test="${number>500}">  
     <c:out value="number is less than 500"></c:out>  
     </c:if>  
</body>
</html>


iii) c:import Tag

It is used to add all functionality of the <include> and provides the URL to the current page.

Example : illustrating the c:import tag

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
<html>
    <head>
        <title>Tag Example</title>
    </head>
<body>
     <h1>import Tag example</h1>  
     <c:import url="http://www.tutorialride.com">
     <c:param name= "Tutorial" value= "true">
     </c:import>
</body>
</html>


iv) c:out Tag

It is used to evaluate the expression and display the results.

Example : illustrating the c:out tag

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
     <head>
         <title>Tag Example</title>
     </head>
<body>
     <h2>out tag example</h2>
     <c:out value="${param.name}" default="TutorialRide" >
</body>
</html>


v) c:redirect Tag

It is used to redirect the page to the given URL.

Example : illustrating the c:redirect tag

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
<html>
    <head>
       <title>Redirect Tag Example</title>
    <head>
<body>
    <c:redirect url="http://tutorialride.com/"></c:redirect>  
</body>
</html>


vi) c:choose, c:when, c:otherwise Tags

These tags are used to perform conditional operation. These are equivalent to if else condition.

Example : illustrating choose, when and otherwise tag

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
    <head>
          <title>Core Tag Example</title>
    </head>
<body>
    <h1>c:when, c:otherwise, c:choose</h1>  
    <c:set value="45" var="num"></c:set>  
    <c:choose>  
       <c:when test="${num%2 == 0}">  
           <c:out value="It is Even Number"></c:out>  
       </c:when>  
       <c:otherwise>  
           <c:out value="It is Odd Number"></c:out>  
       </c:otherwise>  
    </c:choose>
</body>
</html>