JSTL

JSTL stands for JSP standard tag library which is considered as the standard library that contains the ready made tags. We have some tags in JSTL that can be used to remove the scriptlet code from the JSP code. For this purpose these tags provide already implemented some common functionality.

The JSTL is divided into 5 different groups:

 

JSTL core:

This is used to provide a number of tags which may include if, forEach, import, out etc. These tags are further used to support the scripting tasks. The JSTL core tag can be inserted into the JSP page by using the following URL:

URL:

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

 

JSTL Formatting:

This is used to provide such tags that are able to format the date, text, number etc. The JSTL formatting tag can be inserted into the JSP page by using the following URL:

URL:

< % @ taglib prefix = “fmt” uri = “http: //java. sun. com/ jsp/ jstl/ fmt” % >

 

JSTL SQL:

This library is used to support the connection of the relational database. The JSTL SQL library also provides the tags that can perform operations which may include inserting data into the SQL databases, deleting the SQL databases and updating the SQL database. The SQL tag of JSTL can be inserted into the JSP page by using the following URL:

URL:

< % @ taglib prefix = “sql” uri = “http:// java. sun. com/ jsp/ jstl/ sql” % >

 

JSTL XML:

This library is used to support the processing done in XML. It is also used to provide the flow control and other transformation features. The XML tag of JSTL can be inserted into the JSP page by using the following URL:

URL:

< % @ taglib prefix = “x” uri = “http:// java. sun. com/ jsp/ jstl/ xml” % >

 

JSTL Functions:

This library is used to provide support for the manipulation of strings. The JSTL functions tag can be inserted into the JSP page by using the following URL:

URL:

< % @ taglib prefix = “fn” uri = “http:// java. sun. com/ jsp/ jstl/ functions” % >

 

JSTL Core Library:

The core library of JSTL is used to remove the basic java script from the JSP page. For this purpose it provides several tags, the following are some of the important tags in the core library of JSTL:

 

JSTL if tag:

The if tag of JSTL is considered as a conditional tag that is it evaluates conditions or it evaluates those expressions that are based on some condition. The body is only executed when the condition is true in java programming language, similarly when the condition is true in the if tag only then the body will be evaluated. Consider the following example in which we have used the JSTL core library and in the body of html a condition is set using the JSTL if tag if the condition is true then body will be evaluated:

CODE:

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

<html>

<head>

<title>Example of JSTL if tag </title>

</head>

<body>

<c:if test=”${param. name == ‘JSTL’}”>

<p > Tags of: ${param.name} </p>

</c:if>

</body>

</html>

In the above example if the condition that is if the name is equal to JSTL then the body is evaluated.

 

JSTL out tag:

The out tag of JSTL evaluates an expression first and then the result of the expression is written to the JspWriter. Consider the following example in which we have used the JSTL core library out tag to write the name to JspWriter:

CODE:

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

<html>

<head>

<title>Example of out tag </title>

</head>

<body>

<c:out value=”${param.name}” default= “JSTL” />

</body>

</html>

In the above example the attribute value is used to specify that the expression should be written to the JspWriter. The attribute default is used to specify that what value should be written to JspWriter if the result of the expression is null.

 

JSTL forEach tag:

The forEach tag is used to provide iteration within the JSP page. The forEach tag is similar to the for loop in java programming language. We can use the forEach tag to iterate through a number of items in the JSP page. Consider the following example in which we have used the forEach tag to iterate through items within the JSP page:

CODE:

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

< html >

< head >

< title > Example of forEach tag < /title >

< / head >

<body>

<c:forEach var=”message” items=”${errorMsg}” >

<li>${message}</li>

</c:forEach>

</body>

</html>

In the above example the items attribute contains a value which is an expression language expression and this expression has a number of error messages. A variable is used to store each item in iteration, this variable named as message and this variable is also available inside the forEach tag.

 

JSTL choose, when, otherwise tags:

The JSTL tags like choose, when, otherwise are considered as the conditional tags and they are used for the implementation of the operations that are based on some condition. It should be noted that when the condition inside the when tag is evaluated to true only then the body of the when tag is evaluated otherwise the body of the otherwise tag will be evaluated. This structure is similar to the if else condition in java programming language.

The if else if structure can also be implemented by simply using a number of when tags. The when tags are exclusive mutually and hence when the when tag (first tag) is evaluated to true, the control is exited from the choose block. When no condition of “when” tag is evaluated to true then the content inside the “otherwise” block will be executed. Consider the following example to demonstrate this:

CODE:

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

<html>

<head>

<title>Example of choose, when and otherwise tag </title>

</head>

<body>

<c:forEach var=”tutorials” items=”${MyTutorials}” begin=”0″ end=”5″ varStatus=”status”>

<c:choose>

<c:when test=”${status.count % 3 == 0 }”>

<p> Divisible by 3 : ${tutorial.key} </p>

<br/>

</c:when>

<c:when test=”${status.count % 4 == 0 }”>

<p >Divisible by 4 : ${tutorial.key} </p>

<br/>

</c:when>

<c:otherwise>

<p> Not divisible by 3 or by 4 : ${tutorial.key} </p><br/>

</c:otherwise>

</c:choose>

</c:forEach>

</body>

</html>

 

JSTL import tag:

The import tag is used to add contents from other JSP pages to the present JSP page. The URL of the JSP page is provided from where the content is to be imported. The import tag is used as <c: import >. The URL attribute of the import tag can be from outside the web container of JSP. Consider the following example in which the import tag is sued to import an html page and the URL of the page is given:

CODE:

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

< html >

< head >

< title > Example of import tag < / title >

< / head >

< body >

<c: import url = “http:// www.exp.com/asd.html”>>

<c: param name = “ASD” value = “true” / >

< / c: import >

< / body >

< / html >

 

JSTL URL tag:

The JSTL URL tag stores the URL of a page in a variable and can also perform rewriting of the URL if needed. Consider the following example in which we have used the URL tag to store URL of a JSP page:

CODE:

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

<html>

<head>

<title> Example of URL tag </title>

</head>

<body>

<a href='<c:url value= “/first.jsp”/ >’ > First JSP</a>

</body>

</html>

 

JSTL set tag:

This tag stores a variable in a defined scope and it is also used to update the property of the instance of JavaBean. Consider the following example in which we have set the name property of Employee bean:

CODE:

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

< html >

< head >

< title > Example of set tag < /title >

< / head >

< body >

<c: set target = “employee” property = “name” value = “${param. name}” / >

< /body >

</ html >

 

JSTL catch tag:

This tag handles exceptions but the catch tag does not direct the user to the error page. Consider the following example in which the use of the catch tag is demonstrated:

CODE:

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

<html>

<head>

<title>Example of catch tag </title>

</head>

<body>

<c:catch>

<% int x = 0;

int y = 5;

int z = y / x;

%>

</c:catch>

</body>

</html>

In the above example the exception is that the number is divided by zero. This exception will be handled by the catch tag.