CISH6960: Developing Enterprise Applications - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

CISH6960: Developing Enterprise Applications

Description:

CISH6960: Developing Enterprise Applications – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 27
Provided by: kenneth116
Category:

less

Transcript and Presenter's Notes

Title: CISH6960: Developing Enterprise Applications


1
CISH-6960 Developing Enterprise Applications
  • Lecture 4
  • June 5, 2004

2
JSTL
  • The JSP Standard Tag Library

3
Introduction
  • 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

4
JSP 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

5
Indexing
  • 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

6
Operators
  • 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

7
Implicit 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

8
Hello, EL
lthtmlgt ltheadgtlttitlegtHello, EL!lt/titlegtlt/headgt ltb
odygt lth1gtHello, param"name"!lt/h1gt lt/bodygt
lt/htmlgt
9
Some EL Examples
  • a.b
  • myCollection5
  • myBean.myIndexProperty3
  • yourBean.location.path
  • thisthat.member.collection4
  • parampreferences.keyParameter
  • requestScope.forwardedValue
  • sessionScope.mySessionAttribute
  • x ( y z )
  • empty myCollection

10
JSTL
  • 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

11
JSTL 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

12
JSTL Namespaces
13
Using 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

14
Core 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

15
Using 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

16
Formatting 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

17
XML Library
  • Can parse and transform XML
  • Use with ltcimportgt can be very powerful
  • ltcimport url/books.xml varxml /gtltxparse
    docxml varbooklist scopeapplication /gt

18
XML Library
  • XML library understands XPath expressions
  • Major improvement over JSP, and even the standard
    XML parsers in general

19
XML 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
20
Servlet Filters
  • Modifying Requests and Responses

21
Filters
  • 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

22
Filters
23
Uses for Filters
  • Authentication
  • Logging and auditing
  • Data compression
  • Encryption / Decryption
  • XML Transformation
  • Localization
  • Image conversion
  • Caching

24
Filter Interface
  • Three methods
  • init( FilterConfig )
  • destroy()
  • doFilter( ServletRequest, ServletResponse,
    FilterChain )
  • Like the service method in Servlet

25
Filter 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

26
Using 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.
Write a Comment
User Comments (0)
About PowerShow.com