Title: CISH6960: Developing Enterprise Applications
1CISH-6960 Developing Enterprise Applications
2JSTL
- The JSP Standard Tag Library
3Introduction
- Minimize application logic in JSP Pages
- Standard action sequence unwieldy
- JSP 2.0 Expression Language (EL)
- Minimal syntax for reading information
- JSP Standard Tag Library (JSTL) provides custom
actions - Can now replace most scriptlets
4JSP Expressions
- General form, embedded in JSP
- expression
- ltpgtThe value of myBeans name property is
myBean.name.lt/pgt - Can be passed in an attribute value
- ltfmtformatDate valuejob.completion /gt
- EL syntax is mostly intuitive
- Borrows from ECMAScript and XPath
5Indexing
- To read a property, use the dot operator
- a.b
- ltjspgetProperty namea propertyb /gt
- To read an indexed property, use
- myCollection5
- myBean.myIndexedProperty3
- Dot and bracket equivalent
- a.b
- ab
- Bracket useful where dot doesnt work
6Operators
- EL supports arithmetic and logical operators
- Arithmetic
- , -, , /, div, , mod
- Logical
- , , !, and, or, not
- Use parentheses where precedence not obvious
- x (y z)
- Empty operator
- empty A is true if A is null or a collection
with no elements
7Implicit Objects
- Set of objects provided by the container
- All available as simple maps
- Can use either dot or bracket notation
- cookiecustomerName
- Some common objects
- pageContext
- requestScope
- sessionScope
- applicationScope
- param
- initParam
8Hello, EL
lthtmlgt ltheadgtlttitlegtHello, EL!lt/titlegtlt/headgt ltb
odygt lth1gtHello, param"name"!lt/h1gt lt/bodygt
lt/htmlgt
9Some EL Examples
- a.b
- myCollection5
- myBean.myIndexProperty3
- yourBean.location.path
- thisthat.member.collection4
- parampreferences.keyParameter
- requestScope.forwardedValue
- sessionScope.mySessionAttribute
- x ( y z )
- empty myCollection
10JSTL
- Custom tag libraries came from many sources
- Sun decided to adopt a standard set
- JSP 2.0 containers provide native support for
them - Earlier containers use tag library jar and tld
files, like any other tag library - Standard version from Apache
11JSTL Namespaces
- Four distinct libraries, each with its own
namespace and suggested prefix - Namespace prescribed in JSTL spec
- Prefix arbitrary, but very common
- Each imported into a JSP page with the normal lt_at_
taglib gt directive
12JSTL Namespaces
13Using JSTL
- Taglib directive to import library
- Required attribute prefix
- Then use either uri as above, or
- Attribute tagdir points to subdirectory with
tld (tag library descriptor) files - lt_at_ taglib prefixc urihttp//java.sun.com/jst
l/core gt
14Core Library
- Actions
- ltcset target property scope
value /gt - Much like ltjspsetPropertygt
- Can also declare a new variable
- Cannot instantiate an object, so not like
ltjspuseBeangt - ltcif testgtlt/cifgt
- Also available are ltcchoosegt, ltcwhengt, and
ltcotherwisegt - ltcforEach varitem itemssessionScope.cart.i
temsgt lttrgtlttdgtitem.quantitylt/tdgtlt/trgtlt/cfo
rEachgt
15Using JavaBeans with JSTL
- Heres the cool part
- Using EL with beans will find a bean in any scope
- Can use the ltjspuseBeangt tag to instantiate a
bean - Then myBean.property finds the bean regardless
of scope
16Formatting Library
- Used for
- Formatting output
- Localization
- Internationalization
- ltfmtsetLocalegt sets default locale
- ltfmtsetTimeZonegt
- ltfmtformatDate valuenow /gt
- Assumes we already did ltjspuseBean idnow
classjava.util.Date /gt
17XML Library
- Can parse and transform XML
- Use with ltcimportgt can be very powerful
- ltcimport url/books.xml varxml /gtltxparse
docxml varbooklist scopeapplication /gt
18XML Library
- XML library understands XPath expressions
- Major improvement over JSP, and even the standard
XML parsers in general
19XML Parsing
lt_at_ taglib prefix"c" uri"http//java.sun.com/jst
l/core_rt" gt lt_at_ taglib prefix"x"
uri"http//java.sun.com/jstl/xml_rt" gt lttable
bgcolor"FFFFFF" cellpadding"4"
border"1"gt ltcimport var"source"
url"Listings.xml" /gt ltxparse var"doc"
xml"source" /gt ltxforEach var"unit"
select"doc//housingUnitbedrooms
parambedrooms and rent lt paramrent"
gt lttrgt lttdgtltxout select"unit/location/streetA
ddress" /gtlt/tdgt lttdgtltxout select"unit/rent"
/gtlt/tdgt lt/trgt lt/xforEachgt lt/tablegt
20Servlet Filters
- Modifying Requests and Responses
21Filters
- Introduced in Servlet 2.3 spec
- Reusable Java classes which can transform HTTP
requests before they reach their destinations - Configured as a chain
- Each filter acts, then calls next in chain
22Filters
23Uses for Filters
- Authentication
- Logging and auditing
- Data compression
- Encryption / Decryption
- XML Transformation
- Localization
- Image conversion
- Caching
24Filter Interface
- Three methods
- init( FilterConfig )
- destroy()
- doFilter( ServletRequest, ServletResponse,
FilterChain ) - Like the service method in Servlet
25Filter Lifecycle
- Declared in deployment descriptors
- Associated with a servlet or a URL pattern
- Container instantiates single instance
- Container calls init() method
- Filter serves incoming requests through
doFilter() - If container unloads the filter, destroy() is
called
26Using Filters
- Do any required initialization in init()
- In doFilter(), be sure to call chain.doFilter()
to call the next filter in the chain - In deployment descriptor, declare ltfiltergt and
ltfilter-mappinggt - ltfiltergt associates name with class
- ltfilter-mappinggt associates filter name with
ltservlet-namegt or lturl-patterngt - Note that request, response are arguments
- Can add parameters, modify request, etc.