0% found this document useful (0 votes)
3 views46 pages

JSTL

Uploaded by

amudhanmanogaran
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)
3 views46 pages

JSTL

Uploaded by

amudhanmanogaran
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/ 46

JSTL

1.Core tags http://java.sun.com/jsp/jstl/core. The prefix of


core tag is c.

2. Function tags http://java.sun.com/jsp/jstl/functions and


prefix is fn.

3. Formatting tags http://java.sun.com/jsp/jstl/fmt and prefix


is fmt.
4. XML tags http://java.sun.com/jsp/jstl/xml and prefix
is x.
5. SQL tags http://java.sun.com/jsp/jstl/sql and prefix
is sql.
ELEMENT SYNTAX
JSP Expression <%= some Java expression%>
JSP Scriptlet <% some java code 1 to many lines%>
JSP Declaration <%!variable or method declaration%>
JSP comment <%-- --%>
Directives <%@ %>
JSTL Core
• c:out
• c:import
• c:set
• c:remove
• c:catch
• c:if
• c:choose, c:when, c:otherwise
• c:forEach
• c:forTokens
• c:param
• c:redirect
• c:url
<c:out>

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


<html>
<head>
<title>Tag Example</title>
</head>
<body>
<c:out value="${'Welcome to cse'}"/>
</body>
</html>
c:import
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Tag Example</title>
</head>
<body>
<c:import var="data" url="http://www. abc.com"/>
<c:out value="${data}"/>
</body>
</html>
c:set
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"prefix="c" %>

<html>
<head>
<title>Core Tag Example</title>
</head>
<body>
<c:set var="Income" scope="session" value="${4000*4}"/>
<c:out value="${Income}"/>
</body>
</html>
SCOPE
PAGE- For the current page
REQUEST- For the current Request
SESSION- For the current Session
APPLICATION- Accessible everywhere
c:remove
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Core Tag Example</title>
</head>
<body>
<c:set var="income" scope="session" value="${4000*4}"/>
<p>Before Remove Value is: <c:out value="${income}"/></p>
<c:remove var="income"/>
<p>After Remove Value is: <c:out value="${income}"/></p>
</body>
</html>
<c:catch> <c:if>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Core Tag Example</title>
</head>
<body>

<c:catch var ="catchtheException">


<% int x = 2/0;%>
</c:catch>

<c:if test = "${catchtheException != null}">


<p>The type of exception is : ${catchtheException} <br />
There is an exception: ${catchtheException.message}</p>
</c:if>

</body>
</html>
<c:choose>, <c:when>, <c:otherwise> Tag
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Core Tag Example</title>
</head>
<body>
<c:set var="income" scope="session" value="${4000*4}"/>
<p>Your income is : <c:out value="${income}"/></p>
<c:choose>
<c:when test="${income <= 1000}">
Income is not good.
</c:when>
<c:when test="${income > 10000}">
Income is very good.
</c:when>
<c:otherwise>
Income is undetermined...
</c:otherwise>
</c:choose>
</body>
</html>
<c:forEach>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core“ prefix="c" %>

<html>
<head>
<title>Core Tag Example</title>
</head>
<body>
<c:forEach var="j" begin="1" end="3">
Item <c:out value="${j}"/><p>
</c:forEach>
</body>
</html>
• <c:param> URL request parameter to be
specified within URL and it automatically
perform any necessary URL encoding
• The < c:redirect > tag redirects the browser to
a new URL
• The < c:url > tag creates a URL
Functional tags
1. fn:contains()
2. fn:containsIgnoreCase()
3. fn:endsWith()
4. fn:escapeXml()
5. fn:indexOf()
6. fn:trim()
7. fn:startsWith()
8. fn:split()
9. fn:toLowerCase()
10. fn:toUpperCase()
11. fn:substring()
12. fn:substringAfter()
13. fn:substringBefore()
14. fn:length()
15. fn:replace()
fn:contains
<%@ 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 cse"/>
<c:if test="${fn:contains(String, 'cse')}">
<p>Found cse string<p>
</c:if>
<c:if test="${fn:contains(String, ‘cse')}">
<p>Found cse string<p>
</c:if>
</body>
</html>
escapeXml
<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>
fn:endsWith
<c:set var="String" value="Welcome to JSP program
ming"/>
<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>
indexOf
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(stri<ng1, "first")}</p>
<p>Index2- ${fn:indexOf(string2, "second")}</p>
Fn:trim,length
<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>
Fn: Split, Join
<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>
Fn:Substring
<c:set var="string" value="This is the first string."/>
${fn:substring(string, 5, 17)}
Fn:Replace
<c:set var="author" value="Ramesh Kumar"/>
<c:set var="string" value="pqr xyz abc PQR"/>
${fn:replace(author, "Ramesh", "Suresh")}
${fn:replace(string, "pqr", "hello")}
• Tolowercase()
• Touppercase()
• substringAfter()
• substringBefore()
• <c:set var = "string1" value = "This is first
String."/> <c:set var = "string2" value =
"${fn:substringBefore(string1, 'first')}" />
JSTL Formatting tags

1. fmt:parseNumber
2. fmt:timeZone
3. fmt:formatNumber
4. fmt:parseDate
5. fmt:bundle
6. fmt:setTimeZone
7. fmt:setBundle
8. fmt:message
9. fmt:formatDate
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}" maxIntegerdigits=3 />


<p><i>Amount is:</i> <c:out value="${j}" /></p> output: 786.970

<fmt:parseNumber var="j" integerOnly="true" type="number" value="${Amount}" />


<p><i>Amount is:</i> <c:out value="${j}" /></p> output:786
</body>
</html>
Fmt:formatDate
<c:set var="currentDate" value="<%=new java.util.Date()%>"/>
<fmt:formatDate type="date" value="${currentDate}" /> <br/>
<fmt:formatDate type="time" value="${currentDate}" /><br/>
<fmt:formatDate type="both" value="${currentDate}" /> <br/>
<fmt:formatDate pattern="yyyy-MM-DD”value="${currentDate}"/>
Fmt:parseDate-It parses the string representation of
a time and date.

<c:set var="currentDate" value="2014-06-14" />


<fmt:parseDate
value="${currentDate}”var="parsedDate“ />
Current date after parsing: <br/>
<c:out value="${parsedDate}"/>
</body>
</html>
Fmt:bundle
//Java Program Bundle Object
package com.webappprgm;
import java.util.ListResourceBundle; //Class to manage bundle
public class webCategories extends ListResourceBundle {
//Get-method to fetch bundle content
public Object[][] getContents()
{ return contents;
} //Object Array
static final Object[][] contents = {
{ "category.Java", "Java Programming Language" },
{ "category.Algorithms", "Algorithms" },
{ "category.DataStructures", "Data Structures" }
};
}
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
<title>fmt:bundle Tag</title>
</head>
<body>
<fmt:bundle basename=" com.webappprgm . webCategories "
prefix="category.">
<p>${category.Java}</p>
<p>${category.Algorithms}</p>
<p>${category.DataStructures}</p>
</fmt:bundle>
</body>
</html>
setTimeZone, formatDate
<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“ />
</p>
<fmt:setTimeZone value="GMT-10" />
<p><b>Date and Time in GMT-10 time Zone: </b>
<fmt:formatDate value="${date}“ type="both” /></p>
XML tags
• x:out
• x:parse
• x:set
• x:choose
• x:when
• x:otherwise
• x:if
• x:transform
• x:param
x:parse, 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>
<X:set>
<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>
<x:choose> , <x:when>,<x:otherwise>
<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>
<x:if>
<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>
• <x:transform> tag is used in a XML document
for providing the XSL (Extensible Stylesheet
Language) transformation. It is used for
transforming xml data based on XSLT script.
• The <x:param> tag is used to set the
parameter in the XSLT style sheet. It use along
with the transform tag for sending parameter
along with the value.
SQL tags
SQL Tags
Descriptions
sql:setDataSource
It is used for creating a simple data source suitable only for prototyping.
sql:query
It is used for executing the SQL query defined in its sql attribute or the body.
sql:update
It is used for executing the SQL update defined in its sql attribute or in the tag body.
sql:param
It is used for sets the parameter in an SQL statement to the specified value.
sql:dateParam
It is used for sets the parameter in an SQL statement to a specified java.util.Date value.
sql:transaction
It is used to provide the nested database action with a common connection.
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>
sql:query
<sql:query dataSource="${db}" var="rs">
SELECT * from Students;
</sql:query>
<sql:update dataSource="${db}" var="count">
INSERT INTO Students VALUES (154,'Nasreen', 'jaha', 25);
</sql:update>
<sql:Param>

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


url="jdbc:mysql://localhost/test"
user="root" password="1234"/>
<c:set var="StudentId" value="152"/>
<sql:update dataSource="${db}" var="count">
DELETE FROM Students WHERE Id = ?
<sql:param value="${StudentId}" />
</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>
sql:transaction
<%
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>

You might also like