JSTL Core tags provide support for iteration, conditional logic, catch exception, url, forward or redirect response etc. To use JSTL core tags, we should include it in the JSP page like below.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<pre class="prettyprint">
Formatting tags: JSTL Formatting tags are provided for formatting of Numbers, Dates and i18n support through locales and resource bundles. We can include these jstl tags in JSP with below syntax:
<pre class="prettyprint">
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
JSTL Core Tags :
The JavaServer Pages Standard Tag Library (JSTL) is a component of the Java EE Web application development platform. It extends the JSP specification by adding a tag library of JSP tags for common tasks, such as XML data processing, conditional execution, database access, loops and internationalization.
JSTL forEach tag: This tag provides a mechanism for iteration within a JSP page. JSTL forEach tag works similarly to enhanced for loop of Java Technology. You can use this tag to iterate over an existing collection of items. For Example :
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Tag Example</title>
</head>
<body>
<c:forEach var="message" items="${errorMsgs}" >
<li>${message}</li>
</c:forEach>
</body>
</html>
Here the attribute items has its value as an EL expression which is a collection of error messages. Each item in the iteration will be stored in a variable called message which will be available in the body of the forEach tag.
JSTL XML Tags: JSTL XML tags are used to work with XML documents such as parsing XML, trforming XML data and XPath expressions evaluation. Syntax to include JSTL XML tags in JSP page is:
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
SQL tags : JSTL SQL Tags: JSTL SQL Tags provide support for interaction with relational databases such as Oracle, MySql etc. Using JSTL SQL tags we can run database queries, we include these JSTL tags in JSP with below syntax:
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
JSTL choose, when, otherwise tag: These are conditional tags used to implement conditional operations. If the test condition of the when tag evaluates to true, then the content within when tag is evaluated, otherwise the content within the otherwise tag is evaluated.
We can also implement if-else-if construct by using multiple when tag. The when tags are mutually exclusive, that me the first when tag which evaluates to true is evaluated and then, the control exits the choose block. If none of the when condition evaluates to true, then otherwise condition is evaluated. For Example
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Tag Example</title>
</head>
<body>
<c:forEach var="tutorial" items="${MyTutorialMap}" begin="0" end="5" varStatus="status">
<c:choose>
<c:when test="${status.count %2 == 0 }">
<p> Divisible by 2 : ${tutorial.key} </p>
<br/>
</c:when>
<c:when test="${status.count %5 == 0 }">
<p > Divisible by 5 : ${tutorial.key} </p>
<br/>
</c:when>
<c:otherwise>
<p> Neither divisible by 2 nor 5 : ${tutorial.key} </p><br/>
</c:otherwise>
</c:choose>
</c:forEach>
</body>
</html>
JSP - The taglib Directive. Advertisements. The JavaServer Pages API allows you to define custom JSP tags that look like HTML or XML tags and a tag library is a set of user-defined tags that implement custom behavior. ... When you use a custom tag, it is typically of the form <prefix:tagname>.
JSTL catch tag: The JSTL catch tag is used to handle exception and doesn't forward the page to the error page. For Example :
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Tag Example</title>
</head>
<body>
<c:catch>
<% int a = 0;
int b = 10;
int c = b/a;
%>
</c:catch>
</body>
</html>
Formatting Tags Descriptions :
The if tag is a conditional tag used to evaluate conditional expressions. When a body is supplied with if tag, the body is evaluated only when the expression is true. For Example :
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Tag Example</title>
</head>
<body>
<c:if test="${param.name == 'studytonight'}">
<p>Welcome to ${param.name} </p>
</c:if>
</body>
</html>
Function tags: JSTL Functions Tags: JSTL tags provide a number of functions that we can use to perform common operation, most of them are for String manipulation such as String Concatenation, Split String etc. Syntax to include JSTL functions in JSP page is:
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
JSTL out tag: The out tag is used to evaluate an expression and write the result to JspWriter. For Example :
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Tag Example</title>
</head>
<body>
<c:out value="${param.name}" default="StudyTonight" />
</body>
</html>
The value attribute specifies the expression to be written to the JspWriter. The default attribute specifies the value to be written if the expression evaluates null.
The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates core functionality common to many JSP applications. JSTL has support for common, structural tasks such as iteration and conditionals, tags for manipulating XML documents, internationalization tags, and SQL tags.
JSTL Function Tags List :
There JSTL mainly provides 5 types of tags:
JSP Implicit Objects are the Java objects that the JSP Container makes available to developers in each page and developer can call them directly without being explicitly declared. JSP Implicit Objects are also called pre-defined variables.