JSP Tag Lib by Kamalakar Dandu
-
Upload
kamalakar-dandu -
Category
Documents
-
view
222 -
download
0
Transcript of JSP Tag Lib by Kamalakar Dandu
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
1/37
Objectives
Learn new features in custom tag libraries
in JSP1.2
Simple tag
Iteration tag Body tag
Tags Define Scripting Variables Learn new
features in JSP1.2
XML View of JSP syntax Bundling Listeners with Tags
TagLibrary Validator
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
2/37
Tag Libraries
Tag library is a collection of related tagspackaged as a jar file containing:
A tag library descriptor: taglib.tld
Tag handler classes: .class files
Associated resource(s) Standard Tag Libraries: JSP 1.0/1.1 TL (JSR 052):
Actions: data sources and control of presentation
Examples: conditional logic, iterations and so on
Apache project @Jakarta: Jakarta Taglibs Useful TagLibs
Educational and cut-and-paste
Not Standard Tag Library
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
3/37
Components of a Tag Library Tag library descriptor (TLD) file
XML format describes tag name, attributes and tag
handler class
Location specified in JSP file and web.xml
JSP file Imports a tag library which references descriptor file
Defines tag prefix and uses tags
Located in the same directory as the rest of JSP
Tag handler class Must implement javax.servlet.jsp.tagext.Tag Interface
Usually extends TagSupport or BodyTagSupport
Locates in the same directories as servlet class files
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
4/37
Whats the Mechanism?
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
5/37
Three Types of Tags
Simple Tag (Tag interface): For tags does not want to manipulate its body
The body of a tag is evaluated once
Iteration Tag (IterationTag interface): JSP1.2 only
Extends Tagby defining one additional methoddoAfterBody() that controls the reevaluation of its
body,but without manipulation of body content
Body Tag (BodyTag interface):
Extends IterationTagby defining additional methodsthat manipulate the content of evaluating its body
The body of a tag might be evaluated multiple times
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
6/37
Tags
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
7/37
Using Simple Tag in JSP
Some N-Digit Primes
20-digit:
40-digit:
80-digit:
Default (50-digit):
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
8/37
Lifecycle of Simple Tag Handlers
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
9/37
Lifecycle of IterationTag
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
10/37
Need BodyTag
In order to manipulate the content of its
body and evaluate a Tags body multiple
times, need to use a BodyTag
BodyTag interface extends IterationTag
by defining additional methods
Examples:
Iterate rows, query, search results and etc.
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
11/37
Using Body Tag in JSP
...
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
12/37
How Is a BodyTag Invoked?
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
13/37
BodyTag Method Semantics
doStartTag(): Same as Tag semantic, except:
EVAL_BODY_INCLUDE(JSP 1.2) returns: as in IterationTag
EVAL_BODY_BUFFERED (JSP 1.2) or EVAL_BODY_TAG (JPS1.1)returns:
manipulates the body by calling setBodyContent(), doBodyInit(),
doAfterBody, and doEndBody() DoBodyInit():
Prepare to process body
doAfterBody():
Handle processed body in BodyContent
Returns EVAL_BODY_BUFFERED (JSP 1.2) orEVAL_BODY_TAG (JSP 1.1) to reprocess
doEndTag():
Same as Tag Semantics
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
14/37
Repeat Tag: Tags With BodyTagpublic class RepeatTag extends BodyTagSupport {
private int reps;public void setReps(String repeats) {
...
}
public int doAfterBody() {
if (reps-- >= 1) {
BodyContent body = getBodyContent();try {
JspWriter out = body.getEnclosingWriter();
// manipulate bodycontent.
out.println(body.getString());
body.clearBody(); // Clear for next evaluation
} catch(IOException ioe) {
System.out.println("Error in RepeatTag: " + ioe);
}
return(EVAL_BODY_BUFFERED);
} else {
return(SKIP_BODY);}
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
15/37
Using Tag in JSP
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
16/37
mytags
/WEB-INF/codecamp-taglib.tld
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
17/37
Tag Library Descriptor
Used by translator to map custom tags toimplementation
An XML file
Describes a collection of custom Tags Defines:
Version dependencies
Tag(s) and their:
Implementation class Attributes
Tag content model
Authoring tool support (JSP 1.2)
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
18/37
TLD File...
...
repeat
tags.RepeatTag
...
JSP
reps
true
true(r t express value)
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
19/37
Tags Define Scripting Variables
Radio stations that rock:
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
20/37
Specifying the variable TLD subelement
(JSP1.2 only):
member
String
true
NESTED
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
21/37
JSP Pages as XML Documents
Direct Authoring
When content is XML
Manipulation Apply XSLT to generate JSP
Authoring
JSP pages from XML objects
Validation
TagLibraryValidator
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
22/37
XML View of JSP syntax (From 1.2 Specification)
removed
jsp:root element is
annotated with namespace
information. expanded in place
...
...
...
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
23/37
JSP Pages as XML Documents
Flowers available:
aFlower.kind
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
24/37
New DOCTYPE Declaration
DOCTYPE decalration in JSP1.1
DOCTYPE declaration in JSP1.2
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
25/37
Tag LibraryDescriptor Format
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
26/37
Bundling Listeners with Tags
Define listeners with tags in web.xml? Tag library maintenance will be difficult
Much more work to define in web.xml
Define listener declarations in tag library
descriptor (TLD)
Always put TLD files in the WEB-INFdirectory or a subdirectory thereof
somelistener_class
...
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
27/37
Session Counter Sample Code (Listener)public class ActiveSessionCounter
implements HttpSessionListener {private static int sessionCount, sessionLimit;
// Each time a session is created, increment the
running count. If the count exceeds the limit,
print a warning in the log file.
public void sessionCreated(HttpSessionEvent event) {sessionCount++; ...
String warning = getSessionCountWarning(); ...}
public void sessionDestroyed(HttpSessionEvent event) {
...}
public static String getSessionCountWarning() {
String warning = null;
if (sessionCount > sessionLimit) {
warning = ...
return(warning);}
..}
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
28/37
Session Counter Sample Code
//If the number of active sessions is above the limit,
this prints a warning. Otherwise, it does nothing.public class SessionCountWarningTag extends TagSupport {
public int doStartTag() {
try {
String warning =
ActiveSessionCounter.getSessionCountWarning();if (warning != null) {
JspWriter out = pageContext.getOut();
out.println(...); ...
}
} catch(IOException ioe) {
System.out.println("Error printing session warning.");
}
return(SKIP_BODY);
}
}
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
29/37
Session Counter Configuration:
TLD
moreservlets.listeners.ActiveSessionCounter
sessionCount
moreservlets.tags.SessionCountTag
empty
The number of active
sessions.
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
30/37
Session Counter Sample Code
(JSP)
...
Number of sessions in memory:
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
31/37
Session Counter Configuration:
web.xml
/session-count-taglib.tld
/WEB-INF/session-count-taglib-0.9-beta.tld
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
32/37
Checking Syntax with TagLibrary
Validator Need sufficient validation mechanism for
complicated situation
e.g. Checking logic in optional attributes usage
Handling error checking in tags may slow down
execution New class TagLibararyValidator performs arbitrary
page translation time syntax checking
Tag library validators are only triggered when
associated JSP pages are translated into servlets
Modify a validator, make sure also modify andredeploy JSP
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
33/37
Checking Syntax with
TagLibraryValidator Basic Steps
Create a subclass of TagLibraryValidator
Override the validate() method
ValidationMessage[] validate(String prefix, String uri,
PageData page) Return null if syntax is correct
Return ValidationMessage[] if there are errors
Optionally override other methods: initialization
Declare the validator in the TLD file
Try JSP pages that use the tag library If validate() returns null, no action is taken
If validate() returns a non empty array, the servermakes the error messages available
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
34/37
Checking Syntax with TagLibrary
Validator TLD
someValidator
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
35/37
Session Counter Demo
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
36/37
Lab
Understand Event Listener in Tag
Libraries
Run Event Listener in Tag Libraries
-
8/2/2019 JSP Tag Lib by Kamalakar Dandu
37/37
Summary
Develop custom tag libraries with new
capabilities in JSP1.2
Take advantage of JSP 1.2 new features
JSP XML syntax
Bundle event listeners with tag libraries
Validate your JSP syntax