Professional Documents
Culture Documents
http://jets.iti.gov.eg
Ch t 1
Chapter
Custom Tags
http://jets.iti.gov.eg
Chapter 1 Outline
What are the custom tags?
How to Write Custom Tags?
What is the TLD file?
The Tag Handler Life Cycle.
Using Custom tag in a JSP page.
The role of deployment descriptor.
http://jets.iti.gov.eg
http://jets.iti.gov.eg
http://jets.iti.gov.eg
http://jets.iti.gov.eg
http://jets.iti.gov.eg
http://jets.iti.gov.eg
tlibversion
jspversion
shortname
i f
info
uri
tag
JavaTM Education & Technology Services
http://jets.iti.gov.eg
Th
The tlibversion
tlib
i element
l
t specifies
ifi th
the version
i number
b off
the tag library in the following format:
([0-9])* (("." [0-9])? (("." [0-9])? (("." [0-9])?
([0-9])
The shortname element specifies a short name for the tag library.
The value for this element must begin with a letter and must not
contain blank space.
The tag element is the most important element in a tag library. You can
specify more than one tag element in the same TLD file.
http://jets.iti.gov.eg
10
http://jets.iti.gov.eg
11
Example
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP
Tag
Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/webjsptaglibrary_1_1.dtd">
p j
j
j p g
y_ _
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1 1</jspversion>
<jspversion>1.1</jspversion>
<shortname></shortname>
<tag>
<name>myTag</name>
<tagclass>mylibrary.MyCustomTag</tagclass>
</tag>
</taglib>
http://jets.iti.gov.eg
12
IIs the
h Java
J
class
l
that
h is
i linked
li k d to a custom tag and
d gets
invoked every time the JSP container encounters the custom
tag. The tag handler is so named because it handles the
processing of the tag
The tag handler must implement an interface in the
javax servlet jsp tagext package or extend one of the classes in
javax.servlet.jsp.tagext
the same package.
The most important interfaces are :
Tag
Iteration Tag
Body Tag
http://jets.iti.gov.eg
13
Contains
C t i th
the ffollowing
ll i methods
th d
doStartTag
doEndTag
d E dT
getParent
setParent
setPageContext
release
NB:
doStartTag
g return integers
g
which could be SKIP_BODY,,
EVAL_BODY_INCLUDE.
doEndTag return integers which could be SKIP_PAGE,
and EVAL_PAGE
EVAL PAGE
JavaTM Education & Technology Services
Copyright Information Technology Institute
http://jets.iti.gov.eg
14
Example
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class BasicTagHandler
p
g
implements
p
Tag
g{
public void setParent(Tag t) {
}
public void setPageContext(PageContext p) {
}
public void release() {
}
public Tag getParent() {
return null;
}
public
bli iintt d
doStartTag()
St tT () {
return EVAL_BODY_INCLUDE;
}
public int doEndTag() {
return EVAL_PAGE;
}
}
JavaTM Education & Technology Services
Copyright Information Technology Institute
http://jets.iti.gov.eg
15
http://jets.iti.gov.eg
16
http://jets.iti.gov.eg
17
you first
fi t need
d to
t use a taglib
t lib di
directive
ti iin your page.
<%@ taglib uri="tagLibraryURI" prefix="tagPrefix"
%>
The uri attribute specifies an absolute or relative URI
that uniquely
q y identifies the tag
g library
y descriptor
p
associated with this prefix
Next to use it :
<prefix:tagName/>
fi
N
/
<prefix:tagName>body</prefix:tagName>
<m:myTag
<
T number="12"
b "12" power="13"/>
"13"/>
http://jets.iti.gov.eg
18
The JSP container can find the name and location of the
TLD file by looking up the deployment descriptor
This
Thi is
i preferable
f bl but
b t nott obligatory
bli t
Example:
<taglib>
t lib
<taglib-uri> /myTld</taglib-uri>
<taglib-location>/WEB-INF/taglib.tld
<taglib location>/WEB INF/taglib tld </taglib
</taglib-location>
location>
</taglib>
<%@ taglib uri="/myTld"
uri= /myTld prefix=
prefix="easy"%>
easy %>
<easy:myTag/>
JavaTM Education & Technology Services
Copyright Information Technology Institute
http://jets.iti.gov.eg
19
Complete Example
http://jets.iti.gov.eg
20
http://jets.iti.gov.eg
21
http://jets.iti.gov.eg
22
http://jets.iti.gov.eg
23
Example
public class PowerTag implements IterationTag {
PageContext pageContext;
private int number;
private int power;
private int counter;
private int result = 1;
public void setNumber(int number) {
this.number = number;}
public
bli void
id setPower(int
tP
(i t power)) {
this.power = power;}
public void setParent(Tag t) {}
public void setPageContext(PageContext p) {
pageContext = p;}
public void release()
p
() {}
public Tag getParent() {
return null;}
public int doStartTag() {
return EVAL_BODY_INCLUDE;}
JavaTM Education & Technology Services
Copyright Information Technology Institute
http://jets.iti.gov.eg
24
Example (contd)
public int doAfterBody() {
counter++;
result *= number;
if (counter == power)
return SKIP_BODY;
SKIP BODY;
else
return EVAL_BODY_AGAIN;}
EVAL BODY AGAIN;}
public int doEndTag() throws JspException {
System.out.println("doEndTag");
try {
JspWriter out = pageContext.getOut();
out println(number + "^"
out.println(number
^ + power + "="
= + result);}
catch(Exception e) {}
return EVAL_PAGE;}}
EVAL PAGE;}}
JavaTM Education & Technology Services
Copyright Information Technology Institute
http://jets.iti.gov.eg
25
Example (contd)
The jsp should take the form :
<%@ taglib uri="/myTLD" prefix="easy"%>
<easy:myTag number="2"
number= 2 power=
power="3">
3 > </easy:myTag >
http://jets.iti.gov.eg
26
Example (contd)
<name>myTag</name>
<tagclass>mypackage PowerTag</tagclass>
<tagclass>mypackage.PowerTag</tagclass>
<attribute>
<name>number</name>
name number /name
<required>true</required>
</attribute>
<attribute>
<name>power</name>
<required>true</required>
</attribute>
</tag>
</taglib>
JavaTM Education & Technology Services
Copyright Information Technology Institute
http://jets.iti.gov.eg
27
A JSP custom
t
tag
t can have
h
a body
b d content,
t t such
h as the
th
following tag:
<%@ taglib uri="/myTLD"
uri= /myTLD prefix=
prefix="x"%>
x %>
<x:theTag>This is the body content</x:theTag>
When you use the Tag and IterationTag interfaces, you
cannot manipulate the body content. You don't even
have access to it.
This
Thi could
ld b
be achieved
hi
db
by:
The BodyTag Interface
The
Th BodyContent
B d C t t Class
Cl
http://jets.iti.gov.eg
28
http://jets.iti.gov.eg
29
http://jets.iti.gov.eg
30
Example
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public
bli class
l
C
CapitalizerTag
it li T extends
t d B
BodyTagSupport
d T S
t{
public int doAfterBody() {
String content = bodyContent.getString();
bodyContent getString();
try{
p
out = bodyContent.getEnclosingWriter();
y
g
g
()
JspWriter
out.print(content.toUpperCase());
}
catch(Exception
t h(E
ti e)) {}
return SKIP_BODY;
}
}
http://jets.iti.gov.eg
31
Lab Exercise
http://jets.iti.gov.eg
32
Assignment
http://jets.iti.gov.eg
33
Ch t 2
Chapter
JSTL
http://jets.iti.gov.eg
34
Chapter 2 Outline
What is JSTL ?
JSTL main packages.
Core Package
JSP Expression Language
Database Package.
http://jets.iti.gov.eg
35
What is JSTL ?
http://jets.iti.gov.eg
36
http://jets.iti.gov.eg
37
http://jets.iti.gov.eg
38
http://jets.iti.gov.eg
39
The
e XML library
b a y includes
c udes tags for
o tthe
e following
o o
g pu
purposes:
poses
Parsing (that is, reading) XML documents
Printing parts of XML documents
Making decisions in your page based on the contents of an XML
document
The SQL library helps you read and write data from
databases
JavaTM Education & Technology Services
Copyright Information Technology Institute
http://jets.iti.gov.eg
40
http://jets.iti.gov.eg
41
Core Package
It depends mainly on
p
Language
g g
Expression
http://jets.iti.gov.eg
42
Expression Language
How Expression Language looks like ?
It starts
t t with
ith ${
Example :
and
d ends
d with
ith }
${ 1+2}
http://jets.iti.gov.eg
43
http://jets.iti.gov.eg
44
${pageScope.username}
${p
g
p
}
${requestScope.username}
${sessionScope.username}
${applicationScope.username}
Example:
${sessionScope.shopping-Cart[0]}
${sessionScope shopping Cart[0]}
${sessionScope.shoppingCart[1]}
http://jets.iti.gov.eg
45
Example
<p>Wow, I know a lot about you...</p>
<p>Your name is ${param.username}/>.</p>
${param.username}/> </p>
<p>Your password is ${param.pw}/>.</p>
p
are ${p
${param.gender}/>.</p>
g
}
p
<p>You
http://jets.iti.gov.eg
46
cookie:
ki JSTL d
doesnt
t give
i you a way tto sett cookies
ki
(backend job) but u can retrieve cookies.
header: read all client header
headerValues: all values
initParam: to access initialization parameters
pageContext: more details about your page
http://jets.iti.gov.eg
47
http://jets.iti.gov.eg
48
(contd)
http://jets.iti.gov.eg
49
http://jets.iti.gov.eg
50
Comparisons supported by EL
http://jets.iti.gov.eg
51
Comparisons supported by EL
(contd)
http://jets.iti.gov.eg
52
http://jets.iti.gov.eg
53
Core Package
(cont.)
http://jets.iti.gov.eg
54
<c:out>
Used for printing results of expressions
Example:
<c:out value="Hi, there!"/>
<c:out
c:out value
value="${username}"
${username} default
default="Nobody"/>
Nobody /
<c:out value="${username}">
Nothing
</c:out>
http://jets.iti.gov.eg
55
<c:set>
Used mainly to create scoped variables
Example:
p
<c:set var="four" value="${3 + 1}"/>
<c:set var="four" scope="session" value="${3 +
1}"/>
<c:set var="eight">
<c:outt value="${4
l
"${4 * 2}"/>
2}"/
</c:set>
http://jets.iti.gov.eg
56
<c:remove >
Used to delete data
Example:
E
l
<c:remove var="doomed" scope="session"/>
Unlike other jstl tags , it will not propagate across
scopes if not found.
http://jets.iti.gov.eg
57
<c:catch >
<c:catch var="numException">
<jsp:scriptlet>int zero = 0;
out.write(3/zero);</jsp:scriptlet>
</c:catch>
http://jets.iti.gov.eg
58
http://jets.iti.gov.eg
59
<c:if>
(contd)
http://jets.iti.gov.eg
60
Example:
<c:if test="${fatalError}">
Im sorry,
I
<c:if test="${user.education==doctorate}">
Dr.
Dr
</c:if>
<c:out value=
value="${user
${user.name}
name}"/>
/>,
but you have committed a fatal error.
</c:if>
http://jets.iti.gov.eg
61
http://jets.iti.gov.eg
62
http://jets.iti.gov.eg
63
Example
<c:choose>
<c:when test="${num==2}">
<li>Error 1 has occurred.</li>
</c:when>
<c:when test=
test="${num==3}">
${num==3} >
<li>Error 2 has occurred.</li>
</c:when>
<c:when test="${num==4}">
<li>Error 3 has occurred.</li>
</c:when>
<c:otherwise>
<li>Everything is fine.</li>
</c:otherwise>
/
h
i
</c:choose>
JavaTM Education & Technology Services
Copyright Information Technology Institute
http://jets.iti.gov.eg
64
1 <c:when>
1h
and
d <c:otherwise>
th
i
ttags cannott appear
outside a <c:choose> tag.
2- There
Theres
s a flip side to this rule: a <c:choose> tag cannot
have any direct children (or nonwhite space text) except
<c:when> or <c:otherwise> tags.
3- If <c:otherwise> occurs, it must follow all the <c:when>
tags; it may not come before any of them.
4 Every <c:choose> must have at least one <c:when> and
4no more than one <c:otherwise>.
http://jets.iti.gov.eg
65
<c:choose>
<p> hiiiiiiiiiiiiiiiiiii </p>
<c:when> </c:when>
<c:otherwise> </c:otherwise>
</c:choose>
<c:choose>
<c:otherwise> </c:otherwise>
<c:when> </c:when>
</c:choose>
http://jets.iti.gov.eg
66
lets you loop over nearly any sensible collection of items that the
expression language returns
Example:
<c:forEach items="${user.medicalConditions}" var="ailment">
<c:out value=
value="${ailment}"/>
${ailment} />
</c:forEach>
<c:forEach> accepts:
Arrays
Collection variables (including Lists and Sets),
Maps, Iterators, and Enumerations.
simple strings.
http://jets.iti.gov.eg
67
http://jets.iti.gov.eg
68
http://jets.iti.gov.eg
69
Example
http://jets.iti.gov.eg
70
http://jets.iti.gov.eg
71
<c:import>
http://jets.iti.gov.eg
72
Example
<c:import url="target.jsp"/>
<c:import url="http://www.cnn.com"/>
You can retrieve files from another web application
by specifying that web applications name using the
<c:import>
i
t tags
t context
t t attribute
tt ib t .
http://jets.iti.gov.eg
73
requested page
http://jets.iti.gov.eg
74
<c:redirect>
http://jets.iti.gov.eg
75
Database Package
We have to use :
<%@ taglib prefix="sql"
uri="http://java.sun.com/jstl/sql"
i "htt //j
/j tl/ l" %>
Should
Sh ld we use JSP tto connectt to
t database?
d t b
?
When
Wh to use d
database
b
iin JSP ?
http://jets.iti.gov.eg
76
http://jets.iti.gov.eg
77
<sql:setDataSource>
http://jets.iti.gov.eg
78
Example
<sql:setDataSource
driver=jdbc.odbc.jdbcDriver"
url="jdbc:odbc:dsn2"
user=jstl password=iti"
var="databaseOne"
scope="session" />
<sql:setDataSource
dataSource="${databaseOne}"
scope="request" />
http://jets.iti.gov.eg
79
<sql:query>
http://jets.iti.gov.eg
80
Example
<sql:query var="result">
SELECT * FROM CUSTOMERS
</sql:query>
<sql:query var="result" sql="SELECT * FROM
CUSTOMERS"/>
CUSTOMERS"/
http://jets.iti.gov.eg
81
<sql:query> (contd)
Accessing
A
i metadata
t d t : u can gett column
l
names and
d row
count through rowCount, columnNames attributes .
Example:
<sql:query var="smartUsers">
SELECT NAME, IQ FROM USERS WHERE IQ > 120
</sql:query>
<table>
<c:forEach items="${smartUsers.rows}" var="row">
<tr>
tr
<td><c:out value="${row.NAME}"/></td>
<td><c:out value="${row.IQ}"/></td>
</tr>
</c:forEach>
</table>
http://jets.iti.gov.eg
82
Examples
<c:forEach items="${requestScope
items="${requestScope.result.rowsByIndex}
result rowsByIndex} var="row
var="row
varStatus="s">
<c:if test="${s.first}">
<table>
<tr> <c:forEach items="${requestScope.result.columnNames}
var="col">
<th><c:out value="${col}"/></th>
value= ${col} /></th>
</c:forEach> </tr>
</c:if>
<tr>
t <c:forEach
f E h items="${row}"
it
"${
}" var="value">
" l "
<td><c:out value="${value}"/></td>
</c:forEach> </tr>
<c:if test="${s.last}">
</table>
</c:if>
</c:forEach>
JavaTM Education & Technology Services
Copyright Information Technology Institute
http://jets.iti.gov.eg
83
<sql:param>
Example
http://jets.iti.gov.eg
84
Import it using :
http://jets.iti.gov.eg
85
<fmt:formatNumber>
Used to Printing numbers
Differ than <c:out>:
Provide some control on how numbers are printed
<fmt:formatNumber>
fmt:formatNumber tag can automatically sense the
browser locale and customize its output.
Example:
<fmt:formatNumber value="${netWorth}"/>
The output would be:
United States 500,000.01
France 500 000,01
Germany
G
500 000 01
500.000,01
Switzerland 500'000.01
JavaTM Education & Technology Services
Copyright Information Technology Institute
http://jets.iti.gov.eg
86
<fmt:formatNumber> attributes
http://jets.iti.gov.eg
87
<fmt:formatDate>
http://jets.iti.gov.eg
88
<fmt:parseNumber>
http://jets.iti.gov.eg
89
<fmt:parseDate>
http://jets.iti.gov.eg
90
<fmt:setLocale>
http://jets.iti.gov.eg
91
http://jets.iti.gov.eg
92
Resource bundles
http://jets.iti.gov.eg
93
http://jets.iti.gov.eg
94