Top Banner

of 86

5 JSTL

May 31, 2018

Download

Documents

suresh1130
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/14/2019 5 JSTL

    1/86

    JSTL

  • 8/14/2019 5 JSTL

    2/86

    9

    8

    7

    6

    SQL Library5

    XML Library4

    Formatting Library3

    Core Library2

    JSTL(JSP Standard Tag Library)1

    Contents

  • 8/14/2019 5 JSTL

    3/86

    Know

    The JSP Standard Tag Library

  • 8/14/2019 5 JSTL

    4/86

    Be Able To

    Use the JSP Standard Library

  • 8/14/2019 5 JSTL

    5/86

    Our aim is to write scriptless JSP. What

    helps you write scriptless JSPs?Are they enough?

    Question?

  • 8/14/2019 5 JSTL

    6/86

    JSTL

    (JSP Standard Tag Library)

    EL and standard actions help us write scriptlessJSP.

    But these are not enough for operations like

    iterating through an array or do some databaselookup. For such operations there are some inbuilt tag

    libraries called JSP Standard Tag Library (JSTL) JSTL tag libraries can be classified into 4 types

    Core library, Formatting library XML library

    SQL library

  • 8/14/2019 5 JSTL

    7/86

    Prerequisites for using JSTL Jar file required for JSTL to be in the classpath of the

    application server jstl.jar : contains the classes for the tags.

    standard.jar: contains all the tld files

    Any JSP file should include the following directive to availrespective JSTL tags for Core JSTL tags for XML Tags for Formatting tags

  • 8/14/2019 5 JSTL

    8/86

    JSTL installation in tomcat

    Search for the standard.jar and jstl.jar files in

    tomcats installation directory.

    In your web application create a directory lib

    inside the WEB-INF .

    Paste the two jar files in /WEB-INF/lib.

    In your jsp page use the respective taglib

    directives.

  • 8/14/2019 5 JSTL

    9/86

    Core Library

    General-purpose

    Conditional

    Iteration

    URL related

  • 8/14/2019 5 JSTL

    10/86

    Used to evaluate an expression and the resulting value is

    written in the tag body. Syntax

    Without body

    With bodydefault value

    If escapeXml is set to true, then XML-confusing characters< ,> ,&, and is automatically converted to theircharacter-entity equivalent , &,', ".

    default

  • 8/14/2019 5 JSTL

    11/86

    Example

    c:out Example


    ABC

    JSTLEx1

  • 8/14/2019 5 JSTL

    12/86

    Used to set the value of a variable or property.

    Syntax

    Using an attribute value

    Using a tag body

    body content

  • 8/14/2019 5 JSTL

    13/86

    Using an attribute value to set a propertyvalue

    Using a tag body to set a property value

    Body content

  • 8/14/2019 5 JSTL

    14/86

    Examplec:set Example

    ${name}


    ${header.host} ${requestScope.name}

    ${name}
    JSTLEx2

  • 8/14/2019 5 JSTL

    15/86

    Cust Name :${cust.name}Cust No. ${cust.eno}


    Cust Name :${cust.name}

    2112Cust No. ${cust.eno}Cust No. ${cust.eno}

  • 8/14/2019 5 JSTL

    16/86

    Removes a variable from the specified scope.

    Syntax:

  • 8/14/2019 5 JSTL

    17/86

    Catches exception thrown by any tag in its body.

    Syntax

    nested actions

  • 8/14/2019 5 JSTL

    18/86

    Example

    c:remve and c:catch Example


    Exception : ${x}

    Prints :

    Exception : org.apache.jasper.JasperException

    JSTLEx3

  • 8/14/2019 5 JSTL

    19/86

    If the value of attribute is true the body is executed.

    Syntax With body

    Without bodybody content

  • 8/14/2019 5 JSTL

    20/86

    Example

    JSTLEx4

    Check The

    NameWelcome to our page!

  • 8/14/2019 5 JSTL

    21/86

    Like the switch statement

    Syntax

    body content ( and

    )

  • 8/14/2019 5 JSTL

    22/86

    java.lang.Thread class orjava.lang.Runnable interface Java Virtual Machine

    javax.http.servlet package

    Java Server Pages. Noting selected

    Example

    JSTLEx5

  • 8/14/2019 5 JSTL

    23/86

  • 8/14/2019 5 JSTL

    24/86

  • 8/14/2019 5 JSTL

    25/86

    Example Book List ${i}

    You have selected the following

    books.

    ${book}

    JSTLEx6

  • 8/14/2019 5 JSTL

    26/86

    Used to loop over tokenized elements of a string like

    StringTokenizer. Syntax

    Body content

    Example

    ${i}

    JSTLEx7

  • 8/14/2019 5 JSTL

    27/86

    Used likebut with the exception toinclude pages even out of the same servlet context.

    Syntax

    The resource to be included in the specified variable

    name

    optional body content

  • 8/14/2019 5 JSTL

    28/86

    The resource

    body content where varReader isconsumed by another action

  • 8/14/2019 5 JSTL

    29/86

    Used with , and

    Syntax

    Without body

    With body

    parameter value

  • 8/14/2019 5 JSTL

    30/86

    Example

    c:import Example

    Including page

    We will define a custom tag to

    read this document

    JSTLEx9

    show jsp

  • 8/14/2019 5 JSTL

    31/86

  • 8/14/2019 5 JSTL

    32/86

  • 8/14/2019 5 JSTL

    33/86

    if(r== null) {throw new JspException("Error! ");

    }try {pageContext.getOut().print("Reading the page....
    ");while((character = r.read()) != -1)

    pageContext.getOut().print((char)character);}catch(java.io.IOException ex) {throw new JspException(ex.getMessage());

    }return SKIP_BODY;}}

  • 8/14/2019 5 JSTL

    34/86

  • 8/14/2019 5 JSTL

    35/86

    Example

  • 8/14/2019 5 JSTL

    36/86

    Used to send HTTP redirect. Similar to

    HttpServletResponse.sendRedirect Syntax

    Without body content

    With query string parameters

    subtags

    Example:

  • 8/14/2019 5 JSTL

    37/86

    XML Library

    Core XML actions

    XML flow control

    Transform actions

    In Tomcat install jaxen and

    saxpath jar files.

  • 8/14/2019 5 JSTL

    38/86

  • 8/14/2019 5 JSTL

    39/86

  • 8/14/2019 5 JSTL

    40/86

  • 8/14/2019 5 JSTL

    41/86

    Reading XML doc

    Data Collected from a .xmlfile.

    index.jsp

  • 8/14/2019 5 JSTL

    42/86

    Book Title:Book Price:

    Other examples of XPATH:Any descendant: $doc//titleSelf: $doc/books/book/.@attribute: $doc//book[@id="997"]/title

    child: $doc/books/book[title='Mastering EJB']

  • 8/14/2019 5 JSTL

    43/86

    Saves the result of the selectXPath expression to ascoped variable. Returned value may be a node set

    (XML fragment), boolean, string, or number.

    Syntax

    Example:

  • 8/14/2019 5 JSTL

    44/86

    FlowControl: Evaluates the XPath expression in the select attribute. If the

    value is true, writes its tag body. Syntax

    Without body content

    With body content

  • 8/14/2019 5 JSTL

    45/86

    FlowControl:

    Marks the beginning of a conditional section like javaswitch statement.

    Syntax

    body content( and subtags)

    Body content conditional block

  • 8/14/2019 5 JSTL

    46/86

    FlowControl:

    Evaluates the given XPath expression and

    repeats its nested its body content over the

    result, setting the context node to each element

    in the iteration. Syntax

    body content

  • 8/14/2019 5 JSTL

    47/86

    Example

    XMLMastering EJB

    A comprehensive book on EJB3.0455The complete reference JSP495

  • 8/14/2019 5 JSTL

    48/86

    P i

  • 8/14/2019 5 JSTL

    49/86

    Price:

    Desc:-->

    jstlEx11

    T f A ti

  • 8/14/2019 5 JSTL

    50/86

    Transform Action:

    Transforms an XML document with an XSLTtransformation.

    Syntax: Without body

  • 8/14/2019 5 JSTL

    51/86

    With a body for the input document andpossibly parameters

    XML document to parse

    [ actions]

  • 8/14/2019 5 JSTL

    52/86

    Sets transformation parameters. Only valid inside

    Syntax

    parametervalue

    E l

  • 8/14/2019 5 JSTL

    53/86

    Example

    XSTL

  • 8/14/2019 5 JSTL

    54/86

  • 8/14/2019 5 JSTL

    55/86

  • 8/14/2019 5 JSTL

    56/86

    Sets the resource bundle.

    Syntax

  • 8/14/2019 5 JSTL

    57/86

    Used to look up a message in a resource bundleby a key.

    Syntax

    Without a body

  • 8/14/2019 5 JSTL

    58/86

    With body for parameters

    subtags

  • 8/14/2019 5 JSTL

    59/86

    With body for key and optional messageparameters

    key optional subtags

  • 8/14/2019 5 JSTL

    60/86

    Used to supply parameters to

    action.

    Syntax

  • 8/14/2019 5 JSTL

    61/86

    Example

    format


    JSTLEx13

  • 8/14/2019 5 JSTL

    62/86

    JSTLEx13\WEB-INF\classes\MessageResources.property

    Content

    welcome.title=Welcome to the world of JSTLformatting tag

    welcome.user=Hello {0}

  • 8/14/2019 5 JSTL

    63/86

    Used to set the localization context, based

    on the specified resource bundle, to beused within the body content of this tag.

    Syntax

    body content

    E l

  • 8/14/2019 5 JSTL

    64/86

    Example

    format

    JSTLEx14

    line.red=line.blue=

    MessageResources.property

  • 8/14/2019 5 JSTL

    65/86

  • 8/14/2019 5 JSTL

    66/86

    Sets a specific character encoding (see

    http://www.iana.org/assignments/character-sets) to

    decode request parameters.

    Omitting a value indicates to use automatic detection

    of the proper encoding Syntax

    Example:

  • 8/14/2019 5 JSTL

    67/86

    A wrapper to temporarily set the time zone.

    Syntax

    body content

  • 8/14/2019 5 JSTL

    68/86

    Binds the specific time zone to a variable.

    Syntax

    Example

  • 8/14/2019 5 JSTL

    69/86

    Example

    format Pacific Time:


    Local Time:

    f t f tN b

  • 8/14/2019 5 JSTL

    70/86

    Used to format a numeric value in a locale-

    sensitive or customized manner as anumber,currency, or percentage.

    Syntax

  • 8/14/2019 5 JSTL

    71/86

    [maxIntegerDigits=maxIntDigits][minIntegerDigits=minIntDigits][maxFractionDigits=maxFracDigits]

    [minFractionDigits=minFracDigits]

    [var=varName][scope={page|request|session|

    application}]/>

  • 8/14/2019 5 JSTL

    72/86

    Used to parse the string representation of numbers,currencies, and percentages that were formatted in alocale-sensitive or customized manner.

    Syntax

    E l

  • 8/14/2019 5 JSTL

    73/86

    Example

    format

    JSTLEx16

  • 8/14/2019 5 JSTL

    74/86

    Allows the formatting of dates and times in a locale-sensitive or customized manner.

    Syntax

  • 8/14/2019 5 JSTL

    75/86

    Parses the string representation of dates and times that

    were formatted in a locale-sensitive or customized manner. Syntax

  • 8/14/2019 5 JSTL

    76/86

  • 8/14/2019 5 JSTL

    77/86

    SQL Library

    Database access

  • 8/14/2019 5 JSTL

    78/86

    Used to query a database.

    Syntax Without body

    actions

  • 8/14/2019 5 JSTL

    79/86

    Used to export a data source either as a scoped variable or

    as the data source configuration variable

    (javax.servlet.jsp.jstl.sql.dataSource).

    Syntax

  • 8/14/2019 5 JSTL

    80/86

    q p Used to execute an SQL INSERT, UPDATE,or DELETE

    statement. (and SQL statements that return nothing)

    Syntax

    Without body

    With body

    actions

  • 8/14/2019 5 JSTL

    81/86

    Used to set the values of parameter markers(?) in a SQL statement.

    Syntax

  • 8/14/2019 5 JSTL

    82/86

    Used to establishes a transaction context for and subtags.

    Syntax

    and statements

    any nested and actionsmust not specify a dataSource attribute.

  • 8/14/2019 5 JSTL

    83/86

    Likefor values of typejava.util.Date. Subtag ofand.

    Syntax

  • 8/14/2019 5 JSTL

    84/86

    l d l i i

  • 8/14/2019 5 JSTL

    85/86

    .


    Names like XXXtesh

  • 8/14/2019 5 JSTL

    86/86

    ,