+ All Categories
Home > Documents > Advanced Java Server Pages (JSP).pdf

Advanced Java Server Pages (JSP).pdf

Date post: 21-Nov-2015
Category:
Upload: loctran999
View: 20 times
Download: 1 times
Share this document with a friend
94
Java TM Education & Technology Services Advanced Java Server Pages (JSP) (JSP) Java TM Education & Technology Services Copyright© Information Technology Institute http://jets.iti.gov.eg 1
Transcript
  • JavaTM Education & Technology Services

    Advanced Java Server Pages (JSP)(JSP)

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 1

  • Ch t 1Chapter 1

    Custom TagsCustom Tags

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 2

  • 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.

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 3

  • What are the custom tags?

    They are special tags that are used to perform customized actions

    You can separate the presentation part of a JSP page from the business rule implementation (Java code)from the business rule implementation (Java code).

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 4

  • What is wrong with java beans?

    Only three action elementsjsp:useBean,

    jsp:setProperty and getPropertyjsp:setProperty and getProperty.

    In some sit ations e ha e to resort to sing code in a In some situations, we have to resort to using code in a JSP page

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 5

  • What is the main idea of custom tags ?

    JSP 1.1 defined a new feature: custom tags that can be used to perform custom actions.

    Custom tags -unlike java beans - have access to all the objects available to JSP pages and custom tags can beobjects available to JSP pages, and custom tags can be customized using attributes

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 6

  • How to Write Custom Tags?

    There are mainly 4 steps1-Design our own Custom Tag2- Create a TLD file named taglib.tld, and save it in

    the WEB-INF directory. 3 C il d d l th J l ( t3- Compile, and deploy the Java class (your tag

    Handler).4 Create a JSP file import your custom tag then4-Create a JSP file, import your custom tag then

    use it.

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 7

  • The deployment descriptor,JSP,and TLD file.

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 8

  • What is the TLD file?

    A tag library descriptor (TLD) file is an XML document that defines a tag library and its tags. A TLD file is to custom tag handlers what a Web deployment descriptorcustom tag handlers what a Web deployment descriptor is to servlets.

    A TLD file contains the root element ThisA TLD file contains the root element. This element can have the following subelements:

    tlibversion jspversion shortname

    i f info uri tag

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 9

    tag

  • TLD file Elements

    Th tlib i l t ifi th i b f The tlibversion element specifies the version number of the tag library in the following format:

    ([0-9])* (" " [0-9])? (" " [0-9])? (" " [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 info element contains the information used for documentation purposes Its default value is an empty stringpurposes. Its default value is an empty string.

    The uri element specifies the link to an additional source of documentation for the tag library.

    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.

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 10

  • Tag sub elements

    Name (mandatory): specifies an identifier for the tag.T l ( d t ) ifi th f ll lifi d f Tagclass (mandatory): specifies the fully qualified name of the Java class that handles the processing of this tag.

    Teiclass : specifies the helper class for this tag, if there is p p g,one

    Bodycontent: specifies the type of the body content of the tag if any A body content is what appears between thetag, if any. A body content is what appears between the opening and closing tags. This element can have one of the following values: empty, JSP, or tag dependent

    Info: contains an informational description. Attribute : specifies zero or more attributes. The attribute

    element can have three subelements: name, required, andelement can have three subelements: name, required, and rtexprvalue. Only name is a required subelement of the attribute

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 11

  • Example

    p j j j p g y_ _

    1.01 11.1

    myTagmylibrary.MyCustomTag

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 12

  • What is Tag Handler ?

    I h J l h i li k d d Is the Java class that is linked to a custom tag and 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 injavax.servlet.jsp.tagext package or extend one of the classes in the same package.

    The most important interfaces are : Tag Iteration Tag Body Tag Body Tag

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 13

  • The Tag Interface

    C t i th f ll i th d Contains the following methods doStartTag

    d E dT doEndTag getParent

    setParent setParent setPageContext release release NB:

    doStartTag return integers which could be SKIP BODY, g g _ ,EVAL_BODY_INCLUDE.

    doEndTag return integers which could be SKIP_PAGE, and EVAL PAGE

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 14

    and EVAL_PAGE

  • Exampleimport javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*;public class BasicTagHandler implements Tag {p g p g {public void setParent(Tag t) {}public void setPageContext(PageContext p) {}public void release() {}public Tag getParent() {return null;}

    bli i t d St tT () {public int doStartTag() {return EVAL_BODY_INCLUDE;}public int doEndTag() {public int doEndTag() {return EVAL_PAGE;}}

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 15

    }

  • The Tag Handler Life Cycle

    1. The JSP container obtains an instance of the tag handler from the pool or creates a new one. It then calls the setPageContext passing a PageContextcalls the setPageContext, passing a PageContext object representing the JSP page where the custom tag is found.

    2. The JSP container then calls the setParent method. e JS co a e e ca s e se a e e odThis method passes a tag object, which represents the closest tag enclosing the current tag handler.

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 16

  • The Tag Handler Life Cycle (contd)

    3.The JSP container then sets all the attributes in the custom tag if any Attributes are handled likecustom tag, if any. Attributes are handled like properties in a JavaBean, namely by using the getter and setter methods.

    4. Next, the JSP container calls the doStartTag.5. Regardless of the return value of the doStartTag g g

    method, the JSP container next calls the doEndTag method.

    6. The release method is the last method that the JSP container calls.Th JSP i h i f h7. The JSP container returns the instance of the tag handler to a pool for future use.

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 17

  • Using Custom tag in a JSP page

    fi t d t t lib di ti i you first need to use a taglib directive in your page. %>

    The uri attribute specifies an absolute or relative URI that uniquely identifies the tag library descriptor q y g y passociated with this prefix

    Next to use it :fi N /

    body< T b "12" "13"/>

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 18

  • The role of deployment descriptor

    The JSP container can find the name and location of the TLD file by looking up the deployment descriptorThi i f bl b t t bli t This is preferable but not obligatory

    Example:t lib /myTld

    /WEB INF/taglib tld /WEB-INF/taglib.tld

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 19

  • Complete Example

    The tag handler will take the form:public class DoublerTag implements Tag {p g p g {private int number;public void setNumber(int number) {this.number = number;}}PageContext pageContext;public void setParent(Tag t) {}public void setPageContext(PageContext p) {pageContext = p;}public void release() {}public Tag getParent() {return null;}

    S () { {public int doStartTag() {try {JspWriter out = pageContext.getOut();out.println("Double of " + number + " is " + (2 * number));}

    t h(E ti ) {}catch(Exception e) {}return EVAL_BODY_INCLUDE;}public int doEndTag() throws JspException {return EVAL PAGE;}

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 20

    return EVAL_PAGE;}

  • Complete Example (contd)

    The TLD file should take the form

    1.0< h t >

    myTagmylibrary DoublerTagmylibrary.DoublerTag

    numbertruetrue

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 21

    /taglib

  • Complete Example (contd)

    The jsp page should include:

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 22

  • The IterationTag Interface

    The IterationTag interface extends the Tag interface. It adds a new method called doAfterBody and a static

    fi l i t EVAL BODY AGAINfinal integer EVAL_BODY_AGAIN. This method is invoked after the doStartTag method and

    can return either the Tag SKIP BODY orcan return either the Tag.SKIP_BODY or IterationTag.EVAL_BODY_AGAIN.

    If the latter is returned the doAfterBody is called again If If the latter is returned, the doAfterBody is called again. If the return value is Tag.SKIP_BODY, the body will be skipped and the JSP container will call the doEndTag pp gmethod.

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 23

  • Example

    public class PowerTag implements IterationTag {PageContext pageContext;private int number;private int number;private int power;private int counter;private int result = 1;private int result = 1;public void setNumber(int number) {this.number = number;}

    bli id tP (i t ) {public void setPower(int 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() {

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 24

    public int doStartTag() {return EVAL_BODY_INCLUDE;}

  • Example (contd)

    public int doAfterBody() {counter++;result *= number;if (counter == power)return SKIP BODY;return SKIP_BODY;elsereturn EVAL BODY AGAIN;}return EVAL_BODY_AGAIN;}public int doEndTag() throws JspException {System.out.println("doEndTag");try {JspWriter out = pageContext.getOut();out println(number + "^" + power + "=" + result);}out.println(number + ^ + power + = + result);}catch(Exception e) {}return EVAL PAGE;}}

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 25

    return EVAL_PAGE;}}

  • Example (contd)

    The jsp should take the form :

    The tld should take the form :

    1.0tlibversion 1.0 /tlibversion

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 26

  • Example (contd)

    myTagmypackage PowerTagmypackage.PowerTag

    numbername number /nametrue

    powertrue

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 27

  • To manipulate body content

    A JSP t t h b d t t h th A JSP custom tag can have a body content, such as the following tag: This is the body content

    When you use the Tag and IterationTag interfaces, youWhen you use the Tag and IterationTag interfaces, you cannot manipulate the body content. You don't even have access to it.Thi ld b hi d b This could be achieved by: The BodyTag Interface

    Th B d C t t Cl The BodyContent Class

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 28

  • To manipulate body content (contd)

    BodyTag interface has a similar life cycle of IterationTag interface.

    The difference is that the doStartTag method of the tag handler implementing BodyTag can return

    SKIP BODY EVAL BODY INCLUDE SKIP_BODY, EVAL_BODY_INCLUDE, or EVAL_BODY_BUFFERED.

    If the method returns EVAL BODY INCLUDE theIf the method returns EVAL_BODY_INCLUDE, the body is evaluated as it is in IterationTag. If the method returns EVAL_BODY_BUFFERED, a BodyContent bj t i t d th t t th t t 'object is created that represents the custom tag's

    body content Two extra methods are called by the JSP container inTwo extra methods are called by the JSP container in

    tag handlers implementing the BodyTag interface: setBodyContent and doInitBody.

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 29

  • The Support Classes

    The TagSupport Class Implements the IterationTag interfacep g

    The BodyTagSupport ClassE t d T S t i l t B d T Extends TagSupport implements BodyTag interface

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 30

  • Example

    import javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*;

    bli l C it li T t d B d T S t {public class CapitalizerTag extends BodyTagSupport {public int doAfterBody() {String content = bodyContent getString();String content = bodyContent.getString();try{JspWriter out = bodyContent.getEnclosingWriter();p y g g ()out.print(content.toUpperCase());}

    t h(E ti ) {}catch(Exception e) {}return SKIP_BODY;}}}

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 31

  • Lab Exercise

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 32

  • Assignment

    Create your own custom tag , and use in in your jsp y j p

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 33

  • Ch t 2Chapter 2

    JSTLJSTL

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 34

  • Chapter 2 Outline

    What is JSTL ?

    JSTL main packages.

    Core Package

    JSP Expression Language

    Database Package.

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 35

  • What is JSTL ?

    It stands for JSP Standard Tag Library

    What do we need to run JSTL ?

    What is wrong with JSP?

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 36

  • How does JSTL fix JSP problems?

    JSTL t XML th l l d if l bl d JSTL tags are XML, they cleanly and uniformly blend into a page's HTML markup tags.

    The main JSTL tag libraries include most functionalityThe main JSTL tag libraries include most functionality that would be needed in a JSP page.

    JSTL tags are easier for non-programmers and inexperienced programmers to use effectively becauseinexperienced programmers to use effectively, because they do not require any knowledge of programming or Java. JSTL t f bj t i th t d JSTL tags can reference objects in the request and session without knowing the object's type and no casting is required.

    JSP's EL (Expression Language) makes it easy to call getter and setter methods on Java objects.

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 37

  • The relation between JSP and JSTL

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 38

  • JSTL main packages

    Plus the function package which is a set of standardized EL functions.

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 39

  • JSTL main packages (Contd)

    The core library includes tags for the following uses: Accessing and modifying data in memory Making decisions in your pages Looping over data

    The XML library includes tags for the following purposes:e b a y c udes tags o t e o o g pu poses Parsing (that is, reading) XML documents Printing parts of XML documents Making decisions in your page based on the contents of an XMLMaking decisions in your page based on the contents of an XML

    document The formatting and internationalization library includes

    tags for these uses:tags for these uses: Reading and printing numbers Reading and printing dates (with support for time zones) Helping your application work with more than one language Helping your application work with more than one language

    The SQL library helps you read and write data from databases

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 40

  • How to introduce those packages in your code?

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 41

  • Core Package

    It depends mainly on Expression Languagep g g

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 42

  • Expression Language

    How Expression Language looks like ?It t t ith ${ d d ith } It starts with ${ and ends with }

    Example :${ 1+2} ${ 1+2}

    NB: Expression Language can be understood inside JSTL Tags and JSP tags as wellinside JSTL Tags and JSP tags as well.

    Types of data read by EL? Scoped VariablesScoped Variables Request Parameters

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 43

  • What are scoped variable?

    What are types of scoped variables? strings, numbers , booleans and collections which could

    be arrays How EL reads Scoped Variables?

    ${ username }$ ${user.phone}

    ${header["User-Agent"]}

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 44

  • Expression Language (contd)

    The default of scopes: Page-request-session then finally applicationapplication

    To be able to specify your variable scope : ${pageScope.username}${p g p } ${requestScope.username} ${sessionScope.username} ${applicationScope.username}

    Example:${sessionScope shopping Cart[0]} ${sessionScope.shopping-Cart[0]}

    ${sessionScope.shoppingCart[1]}

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 45

  • How to read request parameters?

    Simply by :${param name} ${param.name}

    ${paramValues.name} Example Example

    Wow, I know a lot about you...Your name is ${param.username}/> Your name is ${param.username}/>.Your password is ${param.pw}/>.You are ${param.gender}/>.p ${p g } p

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 46

  • Accessing other data with EL

    Implicit Objects supported by JSTLki JSTL d t i t t ki cookie: JSTL doesnt give you a way to set cookies

    (backend job) but u can retrieve cookies. header: read all client headerheader: read all client header headerValues: all values initParam: to access initialization parametersinitParam: to access initialization parameters pageContext: more details about your page

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 47

  • Page Context elements

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 48

  • Page Context elements (contd)

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 49

  • Mathematical operators supported by EL

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 50

  • Comparisons supported by EL

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 51

  • Comparisons supported by EL (contd)

    Take care : ${ } must include the entire expression. E l f i lid t t t Example of invalid statments:

    ${ ${user.weight} gt ${user.IQ} }${user weight} gt ${user IQ} ${user.weight} gt ${user.IQ}

    Only we can say : ${user weight gt user IQ}${user.weight gt user.IQ}

    Boolean operations and parentheses ${ (param.month == 5 or param.month == 6) and${ (param.month 5 or param.month 6) and

    (param.day == 25) }

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 52

  • To check if a certain parameter exists?

    Use the keyword : empty Example:

    ${empty param.choice} ${empty sessionScope.userName}

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 53

  • Core Package (cont.)

    Now back to core package.

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 54

  • Used for printing results of expressions Example: Example:

    c:out value ${username} default Nobody /

    Nothing

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 55

  • Used mainly to create scoped variables Example:p

    t l "${4 * 2}"/

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 56

  • Used to delete data E l Example:

    Unlike other jstl tags , it will not propagate across scopes if not found.

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 57

  • This action allows you to catch an error in your page,

    int zero = 0;

    out.write(3/zero);

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 58

  • Controlling flow with conditions

    The tag evaluates its test attribute, and if this expression evaluates to false the page skips theexpression evaluates to false, the page skips the body of the tag. On the other hand, if the expression ends up being true.p p g

    The body is processed normally. This body can contain any valid JSP code, including text, HTML tags or other JSP tagstags, or other JSP tags.

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 59

  • (contd)

    You can use tags anywhere in your pageeven in the middle of an HTML tag.

    Example:

    color="red"

    /c:if

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 60

  • Nested tags

    Example:

    IIm sorry,

    DrDr.

    ,but you have committed a fatal error.

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 61

  • Complex conditional tags

    Mutually exclusive conditions with and ,, and . similar to switch statements

    The tag is simple: it takes no attributesThe c:choose tag is simple: it takes no attributes and serves only as a container

    for and tags. Just as HTMLs g tag makes no sense outside a , and make no sense outside

    ha .

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 62

  • Complex conditional tags (contd)

    The tag is similar to : it takes a single test attribute.

    For each tag, no more than one child tag can succeed tag can succeed.

    succeeds only if all its sibling succeeds only if all its sibling tags (those with the same parent tag) have failed.failed.

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 63

  • Example

    Error 1 has occurred.

    Error 2 has occurred.

    Error 3 has occurred.

    Everything is fine.

    / h i

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 64

  • Rules for using the complex conditional tags

    1 h d th i t t1- and tags cannot appear outside a tag.

    2- Theres a flip side to this rule: a tag cannot2- There s a flip side to this rule: a tag cannot have any direct children (or nonwhite space text) except or tags.

    3- If occurs, it must follow all the tags; it may not come before any of them.

    4 Every must have at least one and4- Every must have at least one and no more than one .

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 65

  • Example on Invalid Syntax

    hiiiiiiiiiiiiiiiiiii

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 66

  • Controlling flow with loops

    lets you loop over nearly any sensible collection of items that the expression language returnsexpression language returns

    Example:

    accepts: Arrays Arrays Collection variables (including Lists and Sets), Maps, Iterators, and Enumerations. simple strings.simple strings.

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 67

  • Iterating over strings with

    We can parse strings as followsf T k it " b " d li " "

    can be used with comma delimeter only:

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 68

  • Advanced usage for iteration tags and .

    you can determine information about the current items position within the overall loop: is it first, last, or somewhere in the middle?somewhere in the middle?

    You can loop over only a part of collection using index

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 69

  • Example

    < t l "${l tt }"/>

    f E h b i "1" d "5" " t"

    / f E h

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 70

  • Advanced usage for iteration tags and (contd)

    Looping tags also support a varStatus attribute that lets you recover information about where you are in the overall iterationthe overall iteration

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 71

  • It supercedes , providing all the functionality of the core JSP tag but also adding new features lets you store the retrieved text in a scopedfeatures, lets you store the retrieved text in a scoped variable instead of printing it.

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 72

  • Example

    You can retrieve files from another web application by specifying that web applications name using the

    i t t t t tt ib t tags context attribute.

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 73

  • Communicating with imported pages

    The tag is used to send parameters to the requested page

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 74

  • To redirect to another resource

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 75

  • Database Package

    We have to use : uri="http://java.sun.com/jstl/sql" %>

    Sh ld JSP t t t d t b ? Should we use JSP to connect to database?

    Wh d b i JSP ? When to use database in JSP ?

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 76

  • Two architectures are available

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 77

  • Setting up a database connection you can decide to expose a scoped variable that

    t th d t brepresents the database

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 78

  • Example

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 79

  • Used to perform queries with

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 80

  • Example

    SELECT * FROM CUSTOMERS

  • (contd)

    A i t d t t l d Accessing metadata : u can get column names and row count through rowCount, columnNames attributes .

    Example:Example:

    SELECT NAME, IQ FROM USERS WHERE IQ > 120

    tr

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 82

  • Examples

  • Example

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 84

  • Formatting and internationalizationPackage

    Import it using :

  • Used to Printing numbers Differ than :Differ than :

    Provide some control on how numbers are printed tag can automatically sense thefmt:formatNumber tag can automatically sense the

    browser locale and customize its output. Example:

    The output would be:

    United States 500,000.01 France 500 000,01

    G 500 000 01 Germany 500.000,01 Switzerland 500'000.01

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 86

  • attributes

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 87

  • Used to print date Used to print date Example

    United States May 20, 2002United States May 20, 2002 France 20 mai 2002 Germany 20.05.2002 Netherlands 20-mei-2002 Spain 20-may-2002

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 88

  • Useful only if you need to retrieve a numeric value from a string

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 89

  • Useful to convert a string to a date format

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 90

  • Used to override locales

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 91

  • List of different Locales

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 92

  • Resource bundles

    To define a collection of files used for internationalize your website

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 93

  • References & Recommended Reading

    Core servlets and JSP

    Java for the Web with Servlets, JSP, and EJB

    Manning JSTL in Action

    Sun presentations

    Oracle presentations

    SCJWD study guide

    JavaTM Education & Technology ServicesCopyright Information Technology Institute http://jets.iti.gov.eg 94


Recommended