0% found this document useful (0 votes)
85 views38 pages

JSTL 1

JSTL (JavaServer Pages Standard Tag Library) is a collection of useful JSP tags that help reduce code complexity. It provides tags for common tasks like iteration, conditional logic, formatting, and internationalization. Some key tags include <c:forEach>, <c:if>, <fmt:formatDate>, and <x:parse>. JSTL works by including tag library directives and using predefined tags in JSP pages. It allows developers to build dynamic web pages simply and consistently.

Uploaded by

400 Hassan Raza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views38 pages

JSTL 1

JSTL (JavaServer Pages Standard Tag Library) is a collection of useful JSP tags that help reduce code complexity. It provides tags for common tasks like iteration, conditional logic, formatting, and internationalization. Some key tags include <c:forEach>, <c:if>, <fmt:formatDate>, and <x:parse>. JSTL works by including tag library directives and using predefined tags in JSP pages. It allows developers to build dynamic web pages simply and consistently.

Uploaded by

400 Hassan Raza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Introduction to JSTL In Java

JSTL abbreviated as Java Standard Tag Library, which is a further extension


for JSP (Java Server Pages). JSTL reduced the lines of code for the
developer. This JSTL supports for structural tasks, a common task like
conditional and iteration. These tags used for changing I18N
(Internationalization) tags, SQL tags, XML documents, etc. This also
provides a framework for attaching the already existing custom tags within
the Java Standard Tag Library.

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:

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

JSTL Core Tags List


JSTL Function Tags
The JSTL function provides a number of standard functions, most of these functions are
common string manipulation functions. The syntax used for including JSTL function library in
your JSP is:

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

JSTL Function Tags List


JSTL Formatting tags
The formatting tags provide support for message formatting, number and date formatting etc.
The url for the formatting tags is http://java.sun.com/jsp/jstl/fmt and prefix is fmt.

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:

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


JSTL XML tags
The JSTL XML tags are used for providing a JSP-centric way of manipulating and creating
XML documents.

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:

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

Before you proceed further with the examples, you need to copy the two XML and XPath
related libraries into the <Tomcat Installation Directory>\lib:

Xalan.jar: Download this jar file from the link:

http://xml.apache.org/xalan-j/index.html

XercesImpl.jar: Download this jar file from the link:

1. http://www.apache.org/dist/xerces/j/

JSTL XML tags List

JSTL SQL Tags


The JSTL sql tags provide SQL support. The url for the sql tags
is http://java.sun.com/jsp/jstl/sql and prefix is sql.
The SQL tag library allows the tag to interact with RDBMSs (Relational Databases) such as
Microsoft SQL Server, mySQL, or Oracle. The syntax used for including JSTL SQL tags
library in your JSP is:

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

JSTL SQL Tags List

Examples of JSTL In Java


Following are the examples are given below:
Example #1 – Core tag c:out
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>JSTL Tags</title>
</head>
<body>
<!--THis c:out tag is used for displaying output-->
<c:out value="${'Hello World'}" />
</body>
</html>

Example #2 – Core tag set and c:if tags


Code: SetCIf.jsp

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


<html>
<head>
<title>JSTL Tags</title>
</head>
<body>
<c:set var="money" scope="session" value="${5000*5}"/>
<c:if test="${money > 8000}">
<p>My Salary is: <c:out value="${money}"/><p>
</c:if>
</body>
</html>
Example #3 – Core tag with c:choose tag
Code: CChoose.jsp

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


<html>
<head>
<title>JSTL Tags</title>
</head>
<body>
<c:set var="money" scope="session" value="${5000*5}"/>
<p>Your income is : <c:out value="${money}"/></p>
<c:choose>
<c:when test="${money <= 1000}">
You are paid with good salary.
</c:when>
<c:when test="${money > 10000}">
You are paid with really good salary.
</c:when>
<c:otherwise>
You are paid with low salary .
</c:otherwise>
</c:choose>
</body>
</html>

Example #4 – Core tag with c:when tag


Code: CWhen.jsp

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


<html>
<head>
<title>JSTL Tags</title>
</head>
<body>
<c:set value="214" var="digit"></c:set>
<c:choose>
<c:when test="${digit%2==0}">
<c:out value="${digit} is EVEN digit"></c:out>
</c:when>
<c:otherwise>
<c:out value="${digit} is ODD digit"></c:out>
</c:otherwise>
</c:choose>
</body>
</html>
Example #5 – Core tag with c:foreach tag
Code: CForeach.jsp

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


<html>
<head>
<title>JSTL Tags</title>
</head>
<body>
<c:forEach var="iterator" begin="100" end="110">
Count: <c:out value="${iterator}" />
<p>
</c:forEach>
</body>
</html>

Example #6 – Core tag with c:forTokens tag


Code: CForTokens.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>JSTL Tags</title>
</head>
<body>
<c:forTokens items="My-Name-is-Nathi-Paramesh" delims="-"
var="del">
Word: <c:out value="${del}" />
<p>
</c:forTokens>
</body>
</html>
Example # 7 – Core tag with c:redirect tag
Code: CRedirect.jsp

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


<html>
<head>
<title>JSTL Tags</title>
</head>
<body>
<c:set var="urlName" value="2" scope="request"/>
<c:if test="${urlName<1}">
<c:redirect url="http:/educba.com"/>
</c:if>
<!-- Page directly redirect to gmail because value is greater than 1 -->
<c:if test="${urlName>1}">
<c:redirect url="http://gmail.com"/>
</c:if>
</body>
</html>
Function Tag
Example #1 – Function tag with c:contains tag
Code: FunctionContains.jsp

<%@ 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 Tags</title>
</head>
<body>
<c:set var="StringType"
value="We are learningJSTL" />
<c:if test="${fn:contains(StringType, ‘JSTL’)}">
<h1 style="color: green">Yes Given String found in the value
</h1>
</c:if>
<c:if test="${fn:contains(StringType, ‘JAVA’)}">
<p>No Given String is not found in the value
<p>
</c:if>
</body>
</html>
Example # 2 – Function tag with fn:containsIgnoreCase()
<%@ 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 Functions</title>
</head>
<body>
<c:set var="String" value="Hello world"/>
<c:if test="${fn:containsIgnoreCase(String, 'World')}">
<p>Found World string<p>
</c:if>
<c:if test="${fn:containsIgnoreCase(String, 'WORLD')}">
<p>Found WORLD string<p>
</c:if>
</body>
</html>
Example # 3 – Function tag with fn:endsWith()
<%@ 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 Functions</title>
</head>
<body>
<c:set var="String" value="Welcome to JSP programming"/>
<c:if test="${fn:endsWith(String, 'programming')}">
<p>String ends with programming<p>
</c:if>
<c:if test="${fn:endsWith(String, 'JSP')}">
<p>String ends with JSP<p>
</c:if>
</body>
</html>
Example #4 – Function tag with fn:escapeXml()
<%@ 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 Functions</title>
</head>
<body>
<c:set var="string1" value="It is first String."/>
<c:set var="string2" value="It is <xyz>second String.</xyz>"/>
<p>With escapeXml() Function:</p>
<p>string-1 : ${fn:escapeXml(string1)}</p>
<p>string-2 : ${fn:escapeXml(string2)}</p>
<p>Without escapeXml() Function:</p>
<p>string-1 : ${string1}</p>
<p>string-2 : ${string2}</p>
</body>
</html>
Example #5 – Function tag with fn:indexOf()
<%@ 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 Functions</title>
</head>
<body>
<c:set var="string1" value="It is first String."/>
<c:set var="string2" value="It is <xyz>second String.</xyz>"/>
<p>Index-1 : ${fn:indexOf(string1, "first")}</p>
<p>Index-2 : ${fn:indexOf(string2, "second")}</p>
</body>
</html>

Example # 6 – Function tag with fn:trim()


<%@ 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 Functions</title>
</head>
<body>
<c:set var="str1" value="Welcome to JSP programming "/>
<p>String-1 Length is : ${fn:length(str1)}</p>
<c:set var="str2" value="${fn:trim(str1)}" />
<p>String-2 Length is : ${fn:length(str2)}</p>
<p>Final value of string is : ${str2}</p>
</body>
</html>

Example # 7– Function tag with fn:startsWith()


<%@ 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="msg" value="The Example of JSTL fn:startsWith() Function"/>
The string starts with "The": ${fn:startsWith(msg, 'The')}
<br>The string starts with "Example": ${fn:startsWith(msg, 'Example')}
</body>
</html>
Example #8 – Function tag with fn:split() & fn:join()
<%@ 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 Functions</title>
</head>
<body>
<c:set var="str1" value="Welcome-to-JSP-Programming."/>
<c:set var="str2" value="${fn:split(str1, '-')}" />
<c:set var="str3" value="${fn:join(str2, ' ')}" />
<p>String-3 : ${str3}</p>
<c:set var="str4" value="${fn:split(str3, ' ')}" />
<c:set var="str5" value="${fn:join(str4, '-')}" />
<p>String-5 : ${str5}</p>
</body>
</html>
Example # 9– Function tag with fn:toLowerCase() & fn:toUpperCase()
<%@ 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="string" value="Welcome to JSP Programming"/>
${fn:toLowerCase("HELLO,")}
${fn:toLowerCase(string)}
${fn:toUpperCase("hello,")}
${fn:toUpperCase(string)}
</body>
</html>
Example # 10– Function tag with fn:substring()
String fn:substring(String inputstring, int start, int end)

o start: It is starting position of substring


o end: It is end position of substring
o inputstring: It is string from which a substring needed to be taken
o Return type of the function: String

<%@ 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="string" value="This is the first string."/>
${fn:substring(string, 5, 17)}
</body>
</html>
Example # 11– Function tag with fn:substringBefore() &
fn:substringAfter()
<%@ 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="string" value=" This is the first string "/>
${fn:substringAfter(string, "the")}
${fn:substringBefore(string, "the")}

</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" />

<fmt:parseDate value="${date}" var="parsedDate" pattern="dd-MM-


yyyy" />
<p><c:out value="${parsedDate}" /></p>

</body>
</html>
Output:

Parsed Date:

Fri Aug 12 00:00:00 IST 2016


Example # 4– Formatting tag fmt:setTimeZone
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
<title>fmt:setTimeZone Tag</title>
</head>
<body>
<c:set var="date" value="<%=new java.util.Date()%>" />
<p><b>Date and Time in Indian Standard Time(IST) Zone:</b>
<fmt:formatDate value="${date}"
type="both" timeStyle="long" dateStyle="long" /></p>
<fmt:setTimeZone value="GMT-10" />
<p><b>Date and Time in GMT-10 time Zone: </b><fmt:formatDate
value="${date}"
type="both" timeStyle="long" dateStyle="long" /></p>
</body>
</html>
Output:

Date and Time in Indian Standard Time(IST) Zone:</strong> August 12,


2016 6:27:04 PM IST
Date and Time in GMT-10 time Zone:</strong> August 12, 2016 2:57:04
AM GMT-10:00
Example # 5– Formatting tag fmt:formatDate
<%@ 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:formatDate</title>
</head>
<body>
<h2>Different Formats of the Date</h2>
<c:set var="Date" value="<%=new java.util.Date()%>" />
<p>
Formatted Time :
<fmt:formatDate type="time" value="${Date}" />
</p>
<p>
Formatted Date :
<fmt:formatDate type="date" value="${Date}" />
</p>
<p>
Formatted Date and Time :
<fmt:formatDate type="both" value="${Date}" />
</p>
<p>
Formatted Date and Time in short style :
<fmt:formatDate type="both" dateStyle="short" timeStyle="short"
value="${Date}" />
</p>
<p>
Formatted Date and Time in medium style :
<fmt:formatDate type="both" dateStyle="medium" timeStyle="medium"
value="${Date}" />
</p>
<p>
Formatted Date and Time in long style :
<fmt:formatDate type="both" dateStyle="long" timeStyle="long"
value="${Date}" />
</p>
</body>
</html>
Output:

Different Formats of the Date


Formatted Time : 4:20:50 PM
Formatted Date : Aug 13, 2016
Formatted Date and Time : Aug 13, 2016 4:20:50 PM
Formatted Date and Time in short style : 8/13/16 4:20 PM
Formatted Date and Time in medium style : Aug 13, 2016 4:20:50 PM
Formatted Date and Time in long style : August 13, 2016 4:20:50 PM IST
Example #1 XML tag x:out
<%@ 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>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>

<sql:setDataSource var="db" driver="com.mysql.jdbc.Driver"


url="jdbc:mysql://localhost/test"
user="root" password="1234"/>
</body>
</html>
Example #2 SQL <sql:query>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ 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:query Tag</title>
</head>
<body>
<sql:setDataSource var="db" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/test"
user="root" password="1234"/>
<sql:query dataSource="${db}" var="rs">
SELECT * from Students;
</sql:query>
<table border="2" width="100%">
<tr>
<th>Student ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
<c:forEach var="table" items="${rs.rows}">
<tr>
<td><c:out value="${table.id}"/></td>
<td><c:out value="${table.First_Name}"/></td>
<td><c:out value="${table.Last_Name}"/></td>
<td><c:out value="${table.Age}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>
Example #3 SQL <sql:update>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ 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:update Tag</title>
</head>
<body>
<sql:setDataSource var="db" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/test"
user="root" password="1234"/>
<sql:update dataSource="${db}" var="count">
INSERT INTO Students VALUES (154,'Nasreen', 'jaha', 25);
</sql:update>
<sql:query dataSource="${db}" var="rs">
SELECT * from Students;
</sql:query>
<table border="2" width="100%">
<tr>
<th>Student ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
<c:forEach var="table" items="${rs.rows}">
<tr>
<td><c:out value="${table.id}"/></td>
<td><c:out value="${table.First_Name}"/></td>
<td><c:out value="${table.Last_Name}"/></td>
<td><c:out value="${table.Age}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>
Example #5 SQL tag <sql:dateParam>
<%
Date DoB = new Date("2000/10/16");
int studentId = 151;
%>
<sql:update dataSource="${db}" var="count">
UPDATE Student SET dob = ? WHERE Id = ?
<sql:dateParam value="<%=DoB%>" type="DATE" />
<sql:param value="<%=studentId%>" />
</sql:update>
Example #6 SQL tag Complete Example
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ page import="java.util.Date,java.text.*" %>
<%@ 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: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>

<table border="2" width="100%">


<tr>
<th>Emp ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>DoB</th>
</tr>
<c:forEach var="table" items="${rs.rows}">
<tr>
<td><c:out value="${table.id}"/></td>
<td><c:out value="${table.First_Name}"/></td>
<td><c:out value="${table.Last_Name}"/></td>
<td><c:out value="${table.dob}"/></td>
</tr>
</c:forEach>
</table>

</body>
</html>

You might also like