JSTL 1
JSTL 1
Advantages of JSTL:
Fast Development.
Code Reusability.
No need to use a scriptlet tag.
How does JSTL Work in Java?
JSTL works based on the type of tag we have used in our application. It
consists of around 5 types of tags. They are
Core tags
Function tags
Formatting tags
XML tags
SQL tags
JSTL tags Description:
JSTL Core Tags
The JSTL core tag provides variable support, URL management, flow control etc. The syntax
used for including JSTL core library in your JSP is:
The JSTL formatting tags are used for internationalized web sites to display and format text,
the time, the date and numbers. The syntax used for including JSTL formatting library in your
JSP is:
The xml tags provide flow control, transformation etc. The url for the xml tags
is http://java.sun.com/jsp/jstl/xml and prefix is x. The JSTL XML tag library has custom tags
used for interacting with XML data. The syntax used for including JSTL XML tags library in
your JSP is:
Before you proceed further with the examples, you need to copy the two XML and XPath
related libraries into the <Tomcat Installation Directory>\lib:
http://xml.apache.org/xalan-j/index.html
1. http://www.apache.org/dist/xerces/j/
</body>
</html>
Example # 12– Function tag with fn:length()
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>JSTL fn:length() example</title>
</head>
<body>
<c:set var="str1" value="This is first string"/>
<c:set var="str2" value="Hello"/>
Length of the String-1 is: ${fn:length(str1)}<br>
Length of the String-2 is: ${fn:length(str2)}
</body>
</html>
Example # 13– Function tag with fn:replace()
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Function </title>
</head>
<body>
<c:set var="str1" value="Advanced Java"/>
<c:set var="str2" value="pqr xyz abc PQR"/>
${fn:replace(str1, "Advanced", " Enterprise")}
${fn:replace(str2, "pqr", "hello")}
</body>
</html>
Formatting Tag
Example # 1– Formatting tag fmt:parseNumber
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>fmt:parseNumber tag</title>
</head>
<body>
<h3>The fmt:parseNumber tag Example is:</h3>
<c:set var="Amount" value="786.970" />
<fmt:parseNumber var="j" type="number" value="${Amount}" />
<p><i>Amount is:</i> <c:out value="${j}" /></p>
<fmt:parseNumber var="j" integerOnly="true" type="number"
value="${Amount}" />
<p><i>Amount is:</i> <c:out value="${j}" /></p>
</body>
</html>
Example # 2– Formatting tag fmt:formatNumber
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<html>
<head>
<title>fmt:formatNumber Tag</title>
</head>
<body>
<h3>Formatting of Number:</h3>
<c:set var="Amount" value="9850.14115" />
<p> Formatted Number-1:
<fmt:formatNumber value="${Amount}" type="currency" /></p>
<p>Formatted Number-2:
<fmt:formatNumber type="number" groupingUsed="true"
value="${Amount}" /></p>
<p>Formatted Number-3:
<fmt:formatNumber type="number" maxIntegerDigits="3"
value="${Amount}" /></p>
<p>Formatted Number-4:
<fmt:formatNumber type="number" maxFractionDigits="6"
value="${Amount}" /></p>
<p>Formatted Number-5:
<fmt:formatNumber type="percent" maxIntegerDigits="4"
value="${Amount}" /></p>
<p>Formatted Number-6:
<fmt:formatNumber type="number" pattern="###.###$"
value="${Amount}" /></p>
</body>
</html>
OUTPUT:
Formatting of Number:
Formatted Number-1: $9,850.14
Formatted Number-2: 9,850.141
Formatted Number-3: 850.141
Formatted Number-4: 9,850.14115
Formatted Number-5: 5,014%
Formatted Number-6: 9850.141$
Example # 3– Formatting tag fmt:parseDate
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>fmt:parseDate Tag</title>
</head>
<body>
<h3>Parsed Date:</h3>
<c:set var="date" value="12-08-2016" />
</body>
</html>
Output:
Parsed Date:
<html>
<head>
<title>XML Tags</title>
</head>
<body>
<h2>Vegetable Information:</h2>
<c:set var="vegetable">
<vegetables>
<vegetable>
<name>onion</name>
<price>40/kg</price>
</vegetable>
<vegetable>
<name>Potato</name>
<price>30/kg</price>
</vegetable>
<vegetable>
<name>Tomato</name>
<price>90/kg</price>
</vegetable>
</vegetables>
</c:set>
<x:parse xml="${vegetable}" var="output"/>
<b>Name of the vegetable is</b>:
<x:out select="$output/vegetables/vegetable[1]/name" /><br>
<b>Price of the Potato is</b>:
<x:out select="$output/vegetables/vegetable[2]/price" />
</body>
</html>
Example #2 XML x:parse
novels.xml file:
<books>
<book>
<name>Three mistakes of my life</name>
<author>Chetan Bhagat</author>
<price>200</price>
</book>
<book>
<name>Tomorrow land</name>
<author>NUHA</author>
<price>2000</price>
</book>
</books>
index.jsp,
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<html>
<head>
<title>x:parse Tag</title>
</head>
<body>
<h2>Books Info:</h2>
<c:import var="bookInfo" url="novels.xml"/>
<x:parse xml="${bookInfo}" var="output"/>
<p>First Book title: <x:out select="$output/books/book[1]/name" /></p>
<p>First Book price: <x:out select="$output/books/book[1]/price" /></p>
<p>Second Book title: <x:out select="$output/books/book[2]/name"
/></p>
<p>Second Book price: <x:out select="$output/books/book[2]/price"
/></p>
</body>
</html>
Output:
Books Info:
First Book title: Three mistakes of my life
First Book price: 200
Second Book title: Tomorrow land
Second Book price: 2000
Example #3 XML x:set
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<html>
<head>
<title>x:set Tag</title>
</head>
<body>
<h3>Books Information:</h3>
<c:set var="book">
<books>
<book>
<name>Three mistakes of my life</name>
<author>Chetan Bhagat</author>
<price>200</price>
</book>
<book>
<name>Tomorrow land</name>
<author>Brad Bird</author>
<price>2000</price>
</book>
</books>
</c:set>
<x:parse xml="${book}" var="output"/>
<x:set var="fragment" select="$output/books/book[2]/price"/>
<b>The price of the Tomorrow land book</b>:
<x:out select="$fragment" />
</body>
</html>
Output:
Books Information:
The price of the Tomorrow land book: 2000
Example #4 XML <x:choose>, <x:when>, <x:otherwise>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<html>
<head>
<title>x:choose Tag</title>
</head>
<body>
<h3>Books Information:</h3>
<c:set var="xmltext">
<books>
<book>
<name>Three mistakes of my life</name>
<author>Chetan Bhagat</author>
<price>200</price>
</book>
<book>
<name>Tomorrow land</name>
<author>Brad Bird</author>
<price>2000</price>
</book>
</books>
</c:set>
<x:parse xml="${xmltext}" var="output"/>
<x:choose>
<x:when select="$output//book/author = 'Chetan bhagat'">
Book is written by Chetan bhagat
</x:when>
<x:when select="$output//book/author = 'Brad Bird'">
Book is written by Brad Bird
</x:when>
<x:otherwise>
The author is unknown...
</x:otherwise>
</x:choose>
</body>
</html>
Output:
Books Information:
Book is written by Brad Bird
Example #5 XML <x:if>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<html>
<head>
<title>x:if Tags</title>
</head>
<body>
<h2>Vegetable Information:</h2>
<c:set var="vegetables">
<vegetables>
<vegetable>
<name>onion</name>
<price>40</price>
</vegetable>
<vegetable>
<name>Potato</name>
<price>30</price>
</vegetable>
<vegetable>
<name>Tomato</name>
<price>90</price>
</vegetable>
</vegetables>
</c:set>
<x:parse xml="${vegetables}" var="output"/>
<x:if select="$output/vegetables/vegetable/price < 100">
Vegetables prices are very low.
</x:if>
</body>
</html>
Output:
Vegetable Information:
Vegetables prices are very low.
Example #1 SQL <sql:setDataSource>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<html>
<head>
<title>sql:setDataSource Tag</title>
</head>
<body>
<html>
<head>
<title>sql:transaction Tag</title>
</head>
<body>
<sql:setDataSource var="db" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/test"
user="root" password="1234"/>
<%
Date DoB = new Date("2000/10/16");
int studentId = 151;
%>
<sql:transaction dataSource="${db}">
<sql:update var="count">
UPDATE Student SET First_Name = 'Suraj' WHERE Id = 150
</sql:update>
<sql:update var="count">
UPDATE Student SET Last_Name= 'Saifi' WHERE Id = 153
</sql:update>
<sql:update var="count">
INSERT INTO Student
VALUES (154,'Supriya', 'Jaiswal', '1995/10/6');
</sql:update>
</sql:transaction>
<sql:query dataSource="${db}" var="rs">
SELECT * from Student;
</sql:query>
</body>
</html>