Title: DT2113 Internet Application Development
1DT211/3 Internet Application Development
JSP Actions elements and JSTL
2Introduction
- Previously, identified that JSP provides a
variety of techniques to enable dynamic
processing
3JSP Action elements
- Action elements are an important syntax element
in JSP - They are represented by tags (as is HTML)
- They assist JSP developers to develop in tags
rather thanscriplet programming - Instead of lt, they just use the lt character
(like HTML)
ltprefixaction_namegt body
lt/prefixaction_name gt
4JSP Action elements
- JSP tags have a start tag, a tag body and an
end tag. - The start and end tag have the same name enclosed
in lt and gt - The tag names have an embedded colon character
in them, the part before the colon (prefix)
describes the type of the tag
ltprefixaction_namegt body lt/prefixaction_na
megt
- Part after the is the Action Name
- Note Syntax of Action Elementags is based on XML
5JSP Action elements
- Tags have associated attributes (like HTML e.g.
ltimg src ..) - Full syntax of JSP Action Elements is
- ltprefixaction_name attr1 value attr2
value2 action_bodylt/prefixaction_namegt
If the element doesnt have a body, can lose the
end tag and use shorthand syntax
ofltprefixaction_name attr1 value attr2
value2 /gt
For example
ltjspinclude page"scripts/login.jsp" /gt
6JSP Action elements
Two types
1.
2.
JSP Pre-defined tags
External tag library (e.g. JSTL)
Tags prefix is ltjsp .gt
Tags prefix is whatever developer chooses or
library recommends
Custom and JSTL
(Also called Standard Action Elements)
7JSP Action elements JSP Predefined Tags
- Also called JSP Standard Action Elements
- List of these elements are ltjspforwardgt
- ltjspincludegt
- ltjspparamgt
- ltjspplugingt
- ltjspuseBeangt
- ltjspgetPropertygt
- ltjspsetPropertygt
- See http//java.sun.com/products/jsp/syntax/1.1/sy
ntaxref11.html for detailed attributes and values
Used for Java Beans
8JSP Predefined Tag Example ltjspincludegt
- Standard Action Example ltJSP includegt tag
- Example
- ltHTMLgt ltBODYgt
- Going to include hello.jsp...ltBRgt
- ltjspinclude page"hello.jsp"/gt
- lt/BODYgt
- lt/HTMLgt
- Executes the included JSP page and adds its
output into the this page
9JSP Predefined Tag Example ltjspincludegt
- Whats Difference from Using the include
directive? e.g. _at_ include file hello.jsp
gt - The include directive includes the contents of
another file at compilation time. Good for
including common static code e.g. header file,
footer file. Good on performance ? included only
once. - But, what if including dynamic common code (e.g.
a navigation bar where links are read from the
dB?).. need to re-run the file each time a
request is made -? JSP include - JSP include incorporates the output of the
included JSP file at run time -
10JSP Predefined Tag Example ltjspforwardgt
- Standard Action Example ltJSP forwardgt tag
- Stops processing of one page and starts
processing the page specified by the page
attribute - Example
- ltHTMLgt ltBODYgt
- Error occurredplease waitltBRgt
- ltjspforward pageerrorpage.jsp"/gt
- lt/BODYgt
- lt/HTMLgt
11JSP Predefined Tag Example ltjspparamgt
- Standard Action Example ltJSP paramgt tag
- Can be used to pass parameters when using
ltjspincludegt or ltJSPforwardgt - Example
- ltjspinclude page"login.jsp"gt
- ltjspparam name"username" value"jsmith" /gt
- lt/jspincludegt
Executes a login page jspparam passes in
username to the login page
12JSP Action Elements
JSP pre-defined tags (standard actions elements)
Done
External Tag library -------? Custom
-------? Java Standard tag library
13JSP Action elements External tag libraries
custom
- JSP 1.1. introduced tag libraries, allowing
addition of tags beyond standard action ltjsp gt
tags - Developers can develop their own Custom action
elementsusing custom tag libraries - Custom tag libraries are useful where developers
are working in teams reusabletags can be
supplied to team via custom tag libraries. - The library is identifed in the JSP page using
the lttaglibgtdirective
14JSP Action Elements
JSP pre-defined tags (standard actions elements)
Done
External Tag library -------? Custom
Done -------? Java Standard tag library
15JSP Action elements External tag libraries JSTL
- JSP 1.2 introduced supported for a special tag
librarycalled the JSP Standard Tag Library
(JSTL) - Version 1.0 released in June 2002Version 1.1
released in June 2004 - The JSTL saves programmers from having to develop
custom tag libraries for a range of common
tasks, suchas database access, conditional loops
etc - Enabled developers to produce more maintainable,
simpler JSP code - Important development for JSP technology
16JSTL
- The JSP Standard Tag Library groups actions into
four libraries as follows
more may be added in later releases
17JSTL
- To use any of these libraries in a JSP, need to
declare using the taglib directive in the JSP
page, specifying the URI and the Prefix
Example of declaring use of core library lt_at_
taglib prefix c uri http//java.sun.com/js
p/jstl/core gt
18JSTL Example
Example JSP page using JSTL that outputs 1 to 10
on a webpageusing the ltcforEachgt and ltcoutgt
tags of the core library
lt_at_ taglib uri"http//java.sun.com/jsp/jstl/core"
prefix"c" gt lthtmlgt ltheadgt lttitlegtCount to
10 Example (using JSTL)lt/titlegt lt/headgt
ltbodygt ltcforEach var"i" begin"1" end"10"
step"1"gt ltcout value"i" /gt ltbr /gt
lt/cforEachgt lt/bodygt lt/htmlgt
A taglib directive declare use of core library
JSTL tag examples
19JSTL Example ltcforeachgt
Looking more closely at ltcforEach taggt ..
ltcforEach var"i" begin"1" end"10" step"1"gt
ltcout value"i" /gt ltbr /gt
lt/cforEachgt lt/bodygt lt/htmlgt
The ltforEachgt tag enables loop logic. In this
case, will lookthrough 10 times. Equivalent to
java for loop Closing tag ltcforEachgt placed
after the end of body of loop
20JSTL Example ltcforeachgt
All JSTL tags have a set of attributes (similar
to HTML tags..) e.g. ltcforeachgt tag has 6
attributes var, items, varStatus,
begin, end, step The full details for each
attribute is in the JSTL specification document
(on distrib). See p 65 for ltcforeachgt tag
definition Willl need to use this document to
verify WHICH tag should be used and HOW is
should be used
21JSTL Example ltcoutgt
ltcoutgt .. outputs a value to webpage.
Usually uses just one attribute
value Examples ltcout value"i" /gt ltcout
valueThe result of 1 2 is 12 /gt ltcout
valueparam.userName" /gt
22JSTL Example ltcifgt
ltcifgt .. evaluates a condition. Uses an
attribute test to hold the condition Example
lt-- Simple if conditions --gt ltcif
test'param.p "someValue"'gt Generate
this template text if p equals someValue lt/cifgt
Example 2 ltcif test'param.p'gt Generate
this template text if p equals "true lt/cifgt
23JSTL Multiple if conditions
An if/else action requires the use of the
ltcchoosegt tag Syntax ltcchoosegt body
content (ltwhengt and ltotherwisegt
subtags) lt/cchoosegt
See page 52 of JSTL specification doc.
24JSTL Multiple if conditions
Uses ltcchoosegt, ltcwhengt and ltcotherwisegt Examp
le ltcchoosegt ltcwhen test'param.p
"0"'gt ltcout value zero recorded/gt
lt/cwhengt ltcwhen test'param.p "1"'gt
Generate this ltcout value single value/gt
lt/cwhengt ltcotherwisegt ltcout value Set
to param.p/gt lt/cotherwisegt lt/cchoosegt
25JSTL Other core ltc..gt actions
Other examples (NOT a complete list!)
ltcsetgt .sets the value of a variable
ltcremovegt .removes a scoped
variable ltccatchgt .catches an exception ltcurlgt
.. encodes a URL ltcimportgt imports the content
of a resource ltcredirectgt.. redirects to another
URL ltcparamgt.. adds a request parameter to other
actions
26JSTL Expression language
- Up to now, could only use Java expressions to
assign dynamic values ? syntax errors common - JSTL now provides an expression language (EL) to
support the tags ? simpler syntax, less errors - The EL is now part of the JSP specification (as
of versions JSP2.0) can be used in JSTL tags or
directly in JSP pages.
27JSTL Expression language
- All EL expressions are evaluated at runtime
- The EL usually handles data type conversion and
null values --? easy to use - An EL expression always starts with a and ends
with a
28JSTL Expression language
- The expression can include
- - literals ( 1, 100 etc)
- - variables
- - implicit variables (more later)
- Examples
-
- ltcout value 123 /gt
- ltcif test param.Address D6 /gt
expression
29JSTL Expression language - operators
! lt
- / or div
gt lt gt
- Logical operators consist of (or and), (or
or), and ! (or not). - The empty operator is a prefix operator that can
used to determine if a value is null or empty.
For example - ltcif testempty param.namegt
- Please specify your name.
- lt/cifgt
30JSTL implicit variables
- The JSTL EL allows these objects to be accessed
as Implicit Variables - Implicit variable are just pre-agreed fixed
variable names that can be used in JSTL
Expressions -
- --? Think of as variables that are
automatically available to your JSP page..
31JSTL Expression language Implicit Objects
- Full set of implicit variables on page 211 of
JSTL documentation (includes header,
pageScope..) or on page 74 of OReillys Java
Server Pages book - Very common implicit object is param
- param refers to parameter passed in a request
message (e.g. information entered into a form by
a user). - e.g. ltcout value param.userName/gt
- Further Examples of using param in next topic
-
32Comparing JSTL vs Scriptlets
JSTL removes complexity by using tags instead of
java code(abstraction) JSP pages using JSTL
usually easier to maintain JSTL allows HTML
tag developers to program JSTL often more
difficult to debug Note Using JSTL does not
eliminate scriplets entirely.. may still need
them sometimes for more complex logic
http//www.informit.com/isapi/product_id27F6F199
-F5FD-474C-AC7F-B3FC2C1F57B6/st94C03A97-1188-48
75-8A06-17743BA224B7/session_id6A766315-391B-4F
55-9AFB-B5425F6196C5/content/articlex.asp
33Info on JSTL
1. Java Server Pages by Hans Bergsten full
reference on all JSTL tags in the Appendix. 2.
Sun tutorial. Contains examples. Chapter 17 is on
JSTL at http//java.sun.com/webservices/docs/1.1
/tutorial/doc/index.html 3. Suns JSTL 1.0 tag
specification on distrib. Good for definition of
each tag. Poor on examples.
34Setting up JSTL in a web application
- JSTL is provided as part of Tomcat 5
- Ensure the two JSTL .jar files are copied into
the WEB-INF\lib directory of your web application - Ensure the taglib directive(s) is in your JSP page