JSP Core tags.pdf core java in jsp notes

sitanagar200 27 views 50 slides Aug 31, 2025
Slide 1
Slide 1 of 50
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50

About This Presentation

JSP Core tags.pdf core java in jsp notes


Slide Content

JSTL Core Tags

JSTL tags are grouped into five major categories:
1.​Core Tags
2.​Formatting Tags
3.​SQL Tags
4.​XML Tags
5.​Function Tags

JSTL Core Tags
Core tags in JSTL are used for iteration, conditional logic, url management, handling exceptions,
redirect, etc. 
●​prefix: c
●​uri: http://java.sun.com/jsp/jstl/core
The following tags are included in the JSTL Core tag library:
●​catch 
●​choose 
●​if
●​import
●​forEach
●​forTokens
●​out
●​otherwise 
●​param
●​redirect 
●​remove 
●​set
●​url 
●​when
c:set
It is used to set a value to a variable that can be used within the specified scope in a JSP. 
●​Tag handler class:  org.apache.taglibs.standard.tag.rt.core.SetTag
●​Body content: jsp
●​Attributes: 
o​var: It is used to provide the name of the variable. 
o​value: It is used to provide the value for the variable. It can contain any
expression. 
o​scope: It is used to set the scope of the variable.
o​target: It is used to provide the target object whose property is to be set.
o​property: It is used to specify the name of the property to be set for the target
object.

Example:

set.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Set Tag</title>
</head>
<body>
<h1>
<c:set var="name" value="John"/>
My Name is
<c:out value="${name}"/>
</h1>
</body>
</html>

Output:

My Name is John
c:remove
removes the specified variable.

Tag handler class: org.apache.taglibs.standard.tag.common.core.RemoveTag

Body-content: empty
Attributes:
var: It is used to specify the variable to be removed.
Example:

remove.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Remove Tag</title>
</head>
<body>
<h1>
<c:set var="name" value="John"/>
My Name is
<c:out value="${name}"/>
<br>
<c:remove var="name"/>
My Name is
<c:out value="${name}"/>
</h1>
</body>
</html>
Output:

My Name is John
My Name is
In the above example, the variable name is removed, therefore, the second <c:out> doesn't
display the name.

c:out
It is used to display the content on the web page similar to the expression tag (<%= %).

Tag handler class: org.apache.taglibs.standard.tag.rt.core.OutTag.
Body content: jsp
Attributes:
value: It is used to provide the value to be displayed.
default: It is used to provide the default value, in case, the value provided is null.
Example:

out.jsp


page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Out Tag</title>
</head>
<body>
<h1>

<c:out value="We are learning JSTL Core Tags"/>
</h1>
</body>
</html>
Output:

We are learning JSTL Core Tags
c:forEach
used to iterate over a collection or an array.

Tag handler class: org.apache.taglibs.standard.tag.rt.core.ForEachTag
Body content: jsp
Attributes:
items: It is used to provide a collection of objects to iterate over.
begin: It is used to specify the beginning of the iteration.
end: It is used to specify the end of the iteration.
step: It is used to provide the steps to be taken between two consecutive iterations.
var: It is used to provide a variable for a current item of the iteration.
Example:

forEach.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>ForEach Tag</title>

</head>
<body>
<h1>
<c:forEach var="i" begin="1" end="10" step="2">
<c:out value="${i}"/>
</c:forEach>
<br>
<%
int[] marks={10,12,15,14,9};
session.setAttribute("marks",marks);
%>
<c:forEach var="mark" items="${marks}">
<c:out value="${mark}"/>
</c:forEach>
</h1>
</body>
</html>
Output:

1 3 5 7 9
10 12 15 14 9
c:import
It is used to include the contents from relative or absolute urls within or outside the server.

Tag handler class: org.apache.taglibs.standard.tag.rt.core.ImportTag
Body content: jsp
Attributes:
url: It is used to specify the url of the resource to be imported.

var: It is used to provide the var in which contents of the imported resource are to be stored.
scope: It is used to specify the scope.
Example:

import.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Import Tag</title>
</head>
<body>
<c:import var="content" url="http://www.google.com"/>
<c:out value="${content}"/>
</body>
</html>
c:param
It is used with <c:import> or <c:redirect> to send parameters in the url to the imported or
redirected pages respectively.

Tag handler class: org.apache.taglibs.standard.tag.rt.core.ParamTag
Body content: jsp
Attributes:
name: It is used to provide the name of the parameter.
value: It is used to provide value for the parameter.

c:redirect
It is used to redirect a page to another url.

Tag handler class: org.apache.taglibs.standard.tag.rt.core.RedirectTag
Body content: jsp
Attributes:
url: It is used to provide the url of the resource to be redirected to.
Example:

redirect.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Redirect Tag</title>
</head>
<body>
<h1>
<c:redirect url="welcome.jsp">
<c:param name="name" value="John"/>
</c:redirect>
</h1>
</body>
</html>
welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome</title>
</head>
<body>
<h1>
Welcome <%= request.getParameter("name") %>!
</h1>
</body>
</html>
Output:

Welcome John!
c:catch
It is used to catch any exception of type Throwable that occurs in the body.

Tag handler class: org.apache.taglibs.standard.tag.common.core.CatchTag
Body-content: jsp
Attributes:
var: used to store the exception thrown by body-content.
Example:

catch.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Catch Tag</title>
</head>
<body>
<h1>
<c:catch var="e">
<%int x=2/0; %>
</c:catch>
<c:if test="${e!=null }">
<c:out value="${e }"></c:out>
</c:if>
<br>
</h1>
</body>
</html>
Output:

java.lang.ArithmeticException:/by zero
c:if
It is used to include the conditional statement in the java server pages. Body content is evaluated
only
when the condition evaluates to true.

Tag handler class: org.apache.taglibs.standard.tag.rt.core.IfTag
Body-content: jsp
Attributes:
test: used to provide the testing condition.
Example:

if.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>If Tag</title>
</head>
<body>
<h1>
<c:set var="marks" value="80"/>
<c:if test="${marks>33 }">
Qualified!!
</c:if>
</h1>
</body>
</html>
Output:

Qualified!!
c:forTokens
It is used to iterate over given tokens separated by the specified delimiters.

Tag handler class: org.apache.taglibs.standard.tag.rt.core.ForTokensTag
Body-content: jsp
Attributes:
items: It is used to provide the collection of objects to iterate over.
begin: It is used to specify the beginning of the iterations.
end: It is used to specify the end of the iterations.
step: It is used to provide the steps to be taken between to consecutive iterations
var: used to provide a variable for a current item of the iteration.
delims: used to provide a set of delimiters.
Example:

forToken.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>ForToken Tag</title>
</head>
<body>
<h1>
<c:forTokens var="token" items="10-20-30-40-50" delims="-">

<c:out value="${token }"/>
</c:forTokens>
</h1>
</body>
</html>
Output:

10 20 30 40 50
c:choose, c:when, c:otherwise
It is used to include a conditional statement on the page. It allows multiple conditions similar to
if-else ladder or switch case statements.

<c:choose>: It marks the beginning of conditional and encloses <c:when> and <c:otherwise>.

Tag handler class: org.apache.taglibs.standard.tag.common.core.ChooseTag
Body content: jsp
Attributes: no attribute
<c:when>: It is used to provide the condition and encloses the body to be executed if the
condition evaluates to true.

Tag handler class: org.apache.taglibs.standard.tag.rt.core.WhenTag
Body content: jsp
Attributes:
test: It is used to provide the condition.
<c:otherwise>: It encloses the content to be executed if all the above conditions evaluate as false.

Tag handler class: org.apache.taglibs.standard.tag.common.core.OtherwiseTag
Body content: jsp
Attributes: no attribute

Example:

choose.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Choose Tag</title>
</head>
<body>
<h1>
<c:set var="marks" value="95"/>
<c:choose>
<c:when test="${marks>90}">A Grade</c:when>
<c:when test="${marks>80}">B Grade</c:when>
<c:when test="${marks>70}">C Grade</c:when>
<c:when test="${marks>60}">D Grade</c:when>
<c:when test="${marks>50}">E Grade</c:when>
<c:otherwise>Not satisfactory</c:otherwise>
</c:choose>

</h1>
</body>
</html>
Output:

A Grade
c:url
It is used to create a formatted url along with parameters using a nested <c:param> tag.

Tag handler class: org.apache.taglibs.standard.tag.rt.core.UrlTag
Body content: jsp
Attributes:
var: It is used to provide the variable that holds the url.
scope: It is used to set the scope.
value: It is used to provide the url to be formatted.
Example:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Url Tag</title>
</head>
<body>
<h1> <a href = "<c:url value = "/jsp/welcome.jsp"/> ">Click here</a> </h1>
</body>
</html>
Output:
Click here

JSTL Function Tags
JSTL Function tags provide various string formatting functions.

prefix: fn
uri: http://java.sun.com/jsp/jstl/functions
Tag handler class: org.apache.taglibs.standard.functions.Functions
Include the <taglib> tag in the JSP as follows

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
The following tags are included in the JSTL function tag library :

boolean contains(String, String)
boolean containsIgnoreCase(String, String)
boolean endsWith(String, String)
String escapeXml(String, String)
int indexOf(String, String)
String join(String[], String)
int length(Object)
String replace(String, String, String)
String[] split(String, String)
boolean startsWith(String, String)
String substring(String, int, int)
String substringAfter(String, String)
String substringBefore(String, String)
String toLowerCase(String)
String toUpperCase(String)
String trim(String)
fn:contains()

It checks whether a string is contained inside the given string.

Syntax: boolean contains(String s1, String s2)
Return value: boolean (true if the string is present within the given string, false otherwise)
Parameters:
s1: String to be processed.
s2: String that must be contained in s1.
Example:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Function-Contains</title>
</head>
<body>
<h1>
<c:set var="name" value="HelloEveryOne"/>
${fn:contains(name,'Hello')}
</h1>
</body>
</html>
Output:

true

fn:containsIgnoreCase()
It checks whether a string is contained inside the given string in a case-insensitive way.

Syntax: boolean containsIgnore(String s1, String s2)
Return value: boolean (true if the string is present within the given string, false otherwise)
Parameters:
s1: String to be processed.
s2: Substring that is to be searched.
Example:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Function-ContainsIgnoreCase</title>
</head>
<body>
<h1>
<c:set var="name" value="GeeksForGeeks"/>
${fn:containsIgnoreCase(name,'geeks')}
</h1>
</body>
</html>
Output:

true
fn:endsWith()
It checks whether a string ends with the given suffix.

Syntax: boolean endsWith(String s1, String s2)
Return value: boolean (true if the string ends with the given suffix, false otherwise)
Parameters:
s1 : string to be processed.
s2 : suffix
Example:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Function-endsWith</title>
</head>
<body>
<h1>
<c:set var="name" value="HelloEveryOne"/>
${fn:endsWith(name,'One')}
</h1>
</body>
</html>
Output:

true
fn:escapeXml()
It is used to escape the characters that can be interpreted as XML.

Syntax: String escapeXml(String s)
Return value: String (String obtained after escaping XML characters)
Parameters:
s: String to be processed.
Example:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Function-EscapeXml</title>
</head>
<body>
<h1>
<c:set var="name" value="<abc>HelloEveryOne</abc>"/>
Without escapeXml : ${name}
With escapeXml : ${fn:escapeXml(name)}
</h1>
</body>
</html>

Output:

Without escaleXml : HelloEveryOne
With escapeXml : <abc>HelloEveryOne</abc>
fn:indexOf()
It finds the index of the first occurrence of a given substring.

Syntax: int indexOf(String s1, String s2)
Return value: int(index of first occurrence of substring if found, - 1 otherwise)
Parameters:
s1: string to be processed.
s2: substring whose index is to be determined.
Example:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Function-indexOf</title>
</head>
<body>
<h1>
<c:set var="name" value="GeeksForGeeks"/>
${fn:indexOf(name,"For")}
</h1>

</body>
</html>
Output:

5
fn:split()
It is used to split a string into an array of substrings based on the given separator.

Syntax: String[] split(String s1, String s2)
Return value: String[] (array of substrings obtained after splitting )
Parameters:
s1: spring that is to be split.
s2: separator
fn:join()
It is used to join an array of String with a given separator.

Syntax: String join (String s1[], String s2)
Return value: String (String formed after joining the elements of the array with a given
separator)
Parameters:
s1: array of strings to be joined.
s2: separator
Example:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Function-Split-Join</title>
</head>
<body>
<h1>
<c:set var="str" value="Hello every one"/>
Original string : ${str} <br>
<c:set var="str1" value="${fn:split(str,' ')}"/>
<c:set var="str2" value="${fn:join(str1,'-')}"/>
String after join : ${str2}
</h1>
</body>
</html>
Output:

Original string : Hello every one
String after join : Hello-every-one
fn:length()
It is used to determine the length of a string or the size of a collection.

Syntax: int length(Object o)
Return value: int (length of string or size of collection object)
Parameters:
String or collection whose length is to be determined.
Example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Function-length</title>
</head>
<body>
<h1>
<c:set var="name" value="GeeksForGeeks"/>
Length : ${fn:length(name)}
</h1>
</body>
</html>
Output:

Length : 13
fn:replace()
It is used to replace all the occurrences of a substring with another substring.

Syntax: String replace(String s1, String s2, String s3)
Return value: String (string obtained after replacements)
Parameters:
s1: string to be processed.
s2: old substring
s3: new substring

Example:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Function-Replace</title>
</head>
<body>
<h1>
<c:set var="name" value="hello"/>
${fn:replace(name,"e","x")}
</h1>
</body>
</html>
Output:

hxllo
fn:startsWith()
It checks whether a string starts with the given prefix.

Syntax: boolean startsWith(String s1, String s2)
Return value: boolean (true if string starts with the given prefix, false otherwise)
Parameters:
s1: string to be processed.

s2: prefix
Example:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Function-startsWith</title>
</head>
<body>
<h1>
<c:set var="name" value="GeeksForGeeks"/>
${fn:startsWith(name,'Geeks')}
</h1>
</body>
</html>
Output:

true
fn:substring()
It returns a substring of a given string between specified start and end indices.

Syntax: String substring(String s, int start, int end)
Return value: String (substring obtained)
Parameters:

s: string whose substring is to be determined.
start: start index of the substring.
end: end index of the substring.
Example:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Function-Substring</title>
</head>
<body>
<h1>
<c:set var="name" value="GeeksForGeeks"/>
${fn:substring(name,5,8)}
</h1>
</body>
</html>
Output:

For
fn:substringAfter()
It is used to return a substring of the given string present after the specified target string.

Syntax: String substringAfter(String s1, String s2)

Return value: String (substring obtained)
Parameters:
s1: string whose substring is to be determined.
s2: target string
Example:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Function-SubstringAfter</title>
</head>
<body>
<h1>
<c:set var="name" value="hello all"/>
${fn:substringAfter(name,"For")}
</h1>
</body>
</html>
Output:

hello
fn:substringBefore()
It is used to return a substring of the given string present before the specified target string.

Syntax: String substringBefore(String s1, String s2)
Return value: String (substring obtained)
Parameters:
s1: string whose substring is to be determined.
s2: target string
Example:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Function-SubstringBefore</title>
</head>
<body>
<h1>
<c:set var="name" value="GeeksForGeeks"/>
${fn:substringBefore(name,"For")}
</h1>
</body>
</html>
Output:

Geeks
fn:toLowerCase()
It is used to convert a given string to a lowercase.

Syntax: String toLowerCase(String s)
Return value: String (String in lowercase)
Parameters:
s: string to be processed.
Example:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Function-toLowerCase</title>
</head>
<body>
<h1>
<c:set var="name" value="Hello everyone"/>
${fn:toLowerCase(name)}
</h1>
</body>
</html>
Output:

Hello everyone
fn:toUpperCase()
It is used to convert a given string to uppercase.

Syntax: String toUpperCase(String s)
Return value: String (String in uppercase)
Parameters:
s: string to be processed.
Example:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Function-toUpperCase</title>
</head>
<body>
<h1>
<c:set var="name" value="GeeksForGeeks"/>
${fn:toUpperCase(name)}
</h1>
</body>
</html>
Output:

GEEKSFORGEEKS
fn:trim()
It is used to remove blank spaces from both ends of the given string.

Syntax: String trim(String s)
Return value: String (String obtained after removal of blank spaces)
Parameters:
s: string to be processed.
Example:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Function-trim</title>
</head>
<body>
<h1>
<c:set var="name" value=" hello all "/>
!${name}! <br>
!${fn:trim(name)}!
</h1>
</body>
</html>
Output:

! hello all !
!hello all!

JSTL stands for JavaServer Pages Standard Tag Library. It is introduced in the JSP 2.0 version to
simplify the JSP.
By using JSTL, JSP becomes a 100% tag-based application. JSTL can provide the group of tags
that are used to perform the tasks in the JSP pages without using the Java code. JSTL tags are
divided into the Five types Formatting tags, Core tags, SQL tags, Function tags, and XML tags.
Now we are discussing the Formatting tags.

The JSTL formatting tags are used for formatting data. It allows the developer to manipulate the
data and display the data in different formats like numbers, currencies, and dates on the JSP
pages.

Prerequisites:
The following are the prerequisites which are used in the JSTL formatting tags:

The Prerequisites for using the JSTL formatting tags are:

Java Environment
Servlet Container
JSTL Library
Taglib Declaration
The Syntax of the 'taglib' directive is shown below:

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

uri: It specified the URI of the tag library.
prefix: It defines the prefix used to the reference tags from the library in the JSP page.
Note: By make sure that the above prerequisites will be met, you can easily use the JSTL
formatting tags with the JSP pages for numbers, format dates, currencies and other data types
according to the requirements of your application.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<h1><title>Date Formatting Example</title><h2>
</head>
<body>
<%-- Assuming myDate is a java.util.Date object --%>
<fmt:formatDate value="${myDate}" pattern="yyyy-MM-dd HH:mm:ss" />
<fmt:formatDate type="time" value="${today}" />
<c:set var="today" value="<%=new java.util.Date()%>" />
<div>
<p>Time: <strong><fmt:formatDate type="time" value="${today}" /></strong></p>
<p>Date: <strong><fmt:formatDate type="date" value="${today}" /></strong></p>
<p>Date & Time: <strong><fmt:formatDate type="both" value="${today}"
/></strong></p>
<p>Date & Time Short: <strong><fmt:formatDate type="both" dateStyle="short"
timeStyle="short" value="${today}" /></strong></p>
<p>Date & Time Medium: <strong><fmt:formatDate type="both" dateStyle="medium"
timeStyle="medium" value="${today}" /></strong></p>
<p>Date & Time Long: <strong><fmt:formatDate type="both" dateStyle="long"
timeStyle="long" value="${today}" /></strong></p>
<p>Date (yyyy-MM-dd): <strong><fmt:formatDate pattern="yyyy-MM-dd"
value="${today}" /></strong></p>
</div>
<%-- Assuming myDate is a java.util.Date object --%>
<fmt:formatDate type="time" value="${today}" var="currentTime" />
<p align="center">Current Time: ${currentTime}</p></body></html>

JSTL SQL Tags
SQL tags in JSTL are used to communicate with a relational database to perform various
database operations.

prefix : sql
uri : http://java.sun.com/jsp/jstl/sql
Include the <taglib> directive in the JSP as follows


<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
Following tags are included in the JSTL SQL tag library :

transaction
query
param
update
dateParam
setDataSource
sql:setDataSource

It is used to create a simple data source by taking url, username, password, and driver class name
as parameters. It is used for further database operations.

Tag handler class: org.apache.taglibs.standard.tag.rt.sql.SetDataSourceTag
Body content: empty
Attributes:
var: It is used to provide the variable which holds the data source generated.
scope: It is used to define the scope.

driver: It is used to provide the name of the driver class using which connection is to be
established.
url: It is used to provide the url associated with the database.
user: It is used to provide the username on whose behalf the connection is to be made.
password: It is used to provide the password for the specified user account.
Example:

setDataSource.jsp:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>SetDataSource</title>
</head>
<body>
<sql:setDataSource var="ds" driver="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost:3306/gfg"
user="root" password="root"/>
${ds}
</body>
</html>
Output:

org.apache.taglibs.standard.tag.common.sql.DataSourceWrapper@f8a78d1

sql:query

It is used to execute the query given in the body or the attribute using the specified data source
and store the results in the variable provided in its attribute. It is used to execute the SELECT
statement.

Tag handler class: org.apache.taglibs.standard.tag.rt.sql.QueryTag
Body content: jsp
Attributes:
var: It is used to provide the variable for storing the results of the query execution.
scope: It is used to set the scope for the variable.
dataSource: It is used to provide the data source that is to be used to execute the query.
sql: It is used to provide the sql query.
startRow: It is used to specify the start index of the row to be included in the result. If not
specified, the result contains rows starting from 0.
maxRows: It is used to specify the maximum number of rows to be included in the result. If not
specified, no limit is imposed.
Example:

select.jsp:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">

<title>Select</title>
</head>
<body>
<sql:setDataSource var="ds" driver="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost:3306/gfg"
user="root" password="root"/>
<sql:query dataSource="${ds}" var="rs">
SELECT * FROM student_details;
</sql:query>
<c:forEach var="student" items="${rs.rows}">
${student.Student_Id} ${student.Student_Name}<br>
</c:forEach>
</body>
</html>
Output:

101 Priya
103 Sneha

student_details
sql:update

It is used to execute the update query given in the body or the attribute using the specified data
source.

Tag handler class: org.apache.taglibs.standard.tag.rt.sql.UpdateTag
Body content: jsp
Attributes:
var: It is used to provide the variable for storing the results of the update query execution.

scope: It is used to set the scope for the variable.
dataSource: It is used to provide the data source that is to be used to execute the query.
sql: It is used to provide the sql query.
Example:

update.jsp:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Update</title>
</head>
<body>
<sql:setDataSource var="ds" driver="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost:3306/gfg"
user="root" password="root"/>
<sql:update dataSource="${ds}" var="rs">
UPDATE student_details SET student_name=? WHERE student_id=?;
<sql:param>Diksha</sql:param>
<sql:param>101</sql:param>
</sql:update>
<c:if test="${rs==1}">

Updated Successfully!
</c:if>
</body>
</html>
Output:

Updated Successfully!

student_details

sql:param

It is nested inside the <sql:query> or <sql:update> tags to provide the query parameters.

Tag handler class: org.apache.taglibs.standard.tag.rt.sql.ParamTag
Body content: jsp
Attributes:
value: It is used to provide the value of the parameter.
Example:

insert.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>

<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert</title>
</head>
<body>
<sql:setDataSource var="ds" driver="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost:3306/gfg"
user="root" password="root"/>
<sql:update dataSource="${ds}" var="rs">
INSERT INTO student_details VALUES(?,?);
<sql:param>104</sql:param>
<sql:param>Karan</sql:param>
</sql:update>
<c:if test="${rs==1}">
Inserted Successfully!
</c:if>
</body>
</html>
Output:

Inserted Successfully!

student_details

sql:dateParam

It is used to provide the date, time, or timestamp parameters in the query.

Tag handler class: org.apache.taglibs.standard.tag.rt.sql.DateParamTag
Body content: empty
Attributes:
value: It is used to provide the value for the parameter.
type: It is used to specify the type of value(date, time, or timestamp).
Example:

insert.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="java.util.Date" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert</title>
</head>
<body>
<sql:setDataSource var="ds" driver="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost:3306/gfg"
user="root" password="root"/>
<sql:update dataSource="${ds}" var="rs">
INSERT INTO emp VALUES(?,?,?,?);
<sql:param>102</sql:param>

<sql:param>Sneha</sql:param>
<sql:param>33000</sql:param>
<sql:dateParam value="<%=new Date() %>"/>
</sql:update>
<c:if test="${rs==1}">
Inserted Successfully!
</c:if>
</body>
</html>
Output:

Inserted Successfully!

emp
sql:transaction

It is used to execute multiple queries as one transaction. All the queries are nested inside the
transaction.

Tag handler class: org.apache.taglibs.standard.tag.rt.sql.TransactionTag
Body content: jsp
Attributes:
dataSource: It is used to provide the data source using which queries are to be executed.
Example:

transaction.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1" import="java.util.Date" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Transaction</title>
</head>
<body>
<sql:setDataSource var="ds" driver="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost:3306/gfg"
user="root" password="root"/>
<sql:transaction dataSource="${ds}">
<sql:update var="rs">
INSERT INTO student_details VALUES(?,?);
<sql:param>105</sql:param>
<sql:param>Jyoti</sql:param>
</sql:update>
<sql:update var="rs">
DELETE FROM student_details WHERE student_id=?;
<sql:param>101</sql:param>
</sql:update>
</sql:transaction>
</body>
</html>

When you send the XML data via HTTP, it makes sense to use JSP to handle incoming and
outgoing XML documents; for example, RSS documents. As an XML document is merely a
bunch of text, creating one through a JSP is much easier than creating an HTML document.

Sending XML from a JSP
You can send the XML content using JSPs the same way you send HTML. The only difference is
that you must set the content type of your page to text/xml. To set the content type, use the
<%@page%> tag, like this −

<%@ page contentType = "text/xml" %>
Following example will show how to send XML content to the browser −

<%@ page contentType = "text/xml" %>

<books>
<book>
<name>Padam History</name>
<author>ZARA</author>
<price>100</price>
</book>
</books>
Access the above XML using different browsers to see the document tree presentation of the
above XML.

Processing XML in JSP
Before you proceed with XML processing using JSP, you will need to copy the following two
XML and XPath related libraries into your <Tomcat Installation Directory>\lib −

XercesImpl.jar − Download it from https://www.apache.org/dist/xerces/j/

xalan.jar − Download it from https://xml.apache.org/xalan-j/index.html

Let us put the following content in books.xml file −

<books>
<book>
<name>Padam History</name>
<author>ZARA</author>
<price>100</price>
</book>

<book>
<name>Great Mistry</name>
<author>NUHA</author>
<price>2000</price>
</book>
</books>
Try the following main.jsp, keeping in the same directory −

<%@ 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>JSTL x:parse Tags</title>
</head>

<body>

<h3>Books Info:</h3>
<c:import var = "bookInfo" url="http://localhost:8080/books.xml"/>

<x:parse xml = "${bookInfo}" var = "output"/>
<b>The title of the first book is</b>:
<x:out select = "$output/books/book[1]/name" />
<br>

<b>The price of the second book</b>:
<x:out select = "$output/books/book[2]/price" />
</body>
</html>
Access the above JSP using http://localhost:8080/main.jsp, the following result will be displayed


Books Info:

The title of the first book is:Padam History

The price of the second book: 2000
Formatting XML with JSP
Consider the following XSLT stylesheet style.xsl −

<?xml version = "1.0"?>
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
version = "1.0">

<xsl:output method = "html" indent = "yes"/>
<xsl:template match = "/">

<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match = "books">
<table border = "1" width = "100%">
<xsl:for-each select = "book">
<tr>
<td>
<i><xsl:value-of select = "name"/></i>
</td>

<td>
<xsl:value-of select = "author"/>
</td>

<td>
<xsl:value-of select = "price"/>
</td>
</tr>
</xsl:for-each>
</table>

</xsl:template>
</xsl:stylesheet>

Now consider the following JSP file −

<%@ 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>JSTL x:transform Tags</title>
</head>

<body>
<h3>Books Info:</h3>
<c:set var = "xmltext">
<books>
<book>
<name>Padam History</name>
<author>ZARA</author>
<price>100</price>
</book>

<book>
<name>Great Mistry</name>
<author>NUHA</author>
<price>2000</price>
</book>
</books>
</c:set>

<c:import url = "http://localhost:8080/style.xsl" var = "xslt"/>
<x:transform xml = "${xmltext}" xslt = "${xslt}"/>
</body>
</html>
The following result will be displayed −

Books Info:

Padam History​ZARA​100
Great Mistry​NUHA​2000
Tags