jspsyntaxref12

Embed Size (px)

Citation preview

  • 8/14/2019 jspsyntaxref12

    1/38

    1

    JavaServer Pages (JSP) v1.2

    Syntax Reference

    Defines standard elements and namesp ace attributes of tag libraries.

    Hidden Comment Documents the JSP page bu t is not inserted into the respon se.

    Declaration Declares a variable or method valid in the scripting language u sed in the p age.

    Expression Contains an expression valid in the scripting language u sed in the page.

    Scriptlet Contains a code fragment v alid in the scripting languag e used in the p age.

    Encloses template d ata.

    Include Directive Includ es a resource of text or code wh en the JSP page is tran slated.

    Page Directive Defines attributes that app ly to an entire JSP p age.

    Taglib Directive Defines a tag library and prefix for the custom tags u sed in th e JSP page. Forwards a request to an HTML file, JSP page, or servlet.

    Inserts the value of a bean property into th e response.

    Includ es a static resource or the result from another w eb component

    Causes the execution of an app let or bean. The applet or bean executes in th especified p lugin.

    If the plugin is not available, the client d isplays a dialog to initiate the d own load

    of the plugin software.

    Sets a bean prop erty value or values.

    Instantiates or references a bean w ith a sp ecific name an d scope.

  • 8/14/2019 jspsyntaxref12

    2/38

    2 JavaServer Pages v1.2 Syntax Reference June 2002

    PrefaceAll tags are case sensitive. A pair of single quotes is equivalent to a pair of d ouble

    quotes. Spaces are not allowed between an equals sign and an attribute value.

    The element s in a JSP p age can b e expressed in JSP syntax or XML syntax. The

    following ru les apply:

    s JSP and XML syntax cannot be mixed within a page.

    s A page in one syntax can includ e or forward to a page in the other syntax.

    s Some elements have attributes w hose value can be comp uted at request time. In

    JSP synt ax, the forma t of a valu e is the sam e as a JSP expression: . In XML syntax, the form at of the v alue is %= expression %.

    Quoting Conventions

    The following outlines quoting conventions for JSP pages expressed in JSP syntax.

    Scripting Elements

    s %> by %\ >

    Template Text

    s

    s

  • 8/14/2019 jspsyntaxref12

    3/38

    3

    Defines standard JSP elements and namespace attributes of tag libraries.

    JSP Syntax

    Non e. How ever, see Taglib Directive.

    XML Syntax

    JSP Page

    Example

    ...

    Description

    A JSP p age in XML syntax h as jsp:root as its root elemen t. Tag libraries u sed

    within th e JSP page are represented in th e root element through xmlns attributes.

    The xmlns:jsp and version attributes are mand atory.

    Attributes

    s xmlns:jsp="http://java.sun.com/JSP/Page"

    Enables the use of the standard elements defined in the JSP specification.

    s version="1.2"

    Specifies the version of the JSP sp ecification the page is u sing.

  • 8/14/2019 jspsyntaxref12

    4/38

    4 JavaServer Pages v1.2 Syntax Reference June 2002

    s xmlns:taglibPrefix="URI"

    Specifies a tag library p refix and URI.

    taglibPrefix precedes the custom tag nam e, for example, public in. Empty prefixes are illegal. If you are developing or using

    custom tags, you cannot use th e tag p refixes jsp, jspx, java, javax, servlet,

    sun, and sunw, as they are reserved by Sun Microsystems.

    URI uniquely locates the TLD that describes the set of custom tags associated

    with the named prefix. URI may be of one of two forms:

    s A URI:

    sA Uniform Resource Locator (URL), as d efined in RFC 2396, available at http://

    www.rfc-editor.org/rfc/rfc2396.txt

    sA Uniform Resource Name (URN), as defined in RFC 2396

    s urn:jsptld:path.

    If the URI is a URI, then the TLD is located by consulting the mapping indicated

    in web.xml extend ed u sing the implicit ma ps in the packaged t ag libraries. IfURI

    is of the form urn:jsptld:path, path is interpreted relative to the root of the

    web ap plication and shou ld resolve to a TLD file directly, or to a JAR file that ha s

    a TLD file at location META-INF/ taglib.tld.

    See Also

    s Taglib Directive

    http://www.rfc-editor.org/rfc/rfc2396.txthttp://www.rfc-editor.org/rfc/rfc2396.txthttp://www.rfc-editor.org/rfc/rfc2396.txthttp://www.rfc-editor.org/rfc/rfc2396.txt
  • 8/14/2019 jspsyntaxref12

    5/38

  • 8/14/2019 jspsyntaxref12

    6/38

    6 JavaServer Pages v1.2 Syntax Reference June 2002

    Declaration

    Declares a variable or method valid in the scripting language u sed in the JSP page.

    JSP Syntax

    XML Syntax

    declaration; [ declaration; ]+ ...

  • 8/14/2019 jspsyntaxref12

    7/38

    Declaration 7

    See Also

    s Scriptlet

    s

    Expression

  • 8/14/2019 jspsyntaxref12

    8/38

    8 JavaServer Pages v1.2 Syntax Reference June 2002

    Expression

    Contains an expression valid in the scripting langu age used in the JSP page.

    JSP Syntax

    XML Syntax

    expression

    Examples

    The map file has entries.

    Good guess, but nope. Try

    numguess.getHint().

    Description

    An expression element contains a scripting language expression that is evaluated,converted to a String, and inserted into the response where the expression app ears

    in the JSP page. Because the value of an expression is converted to a String, you

    can use an expression w ithin a line of text, wh ether or n ot it is tagged w ith HTML,

    in a JSP p age.

    The expression element can contain any expression that is a valid page scripting

    language. When the Java program ming language is the scripting language you d o

    not u se a semicolon to end the expression. How ever, the same expression w ithin a

    scriptlet requires the semicolon; see Scriptlet).

    You can sometim es use expressions as attribu te valu es in JSP elemen ts (see the

    JavaServer Pages Syntax Card). An expression can be comp lex and composed of more

    than one part or expression. The parts of an expression are evaluated in left-to-right

    order.

    See Alsos Declaration

    http://../pdf/card12.pdfhttp://../pdf/card12.pdf
  • 8/14/2019 jspsyntaxref12

    9/38

    Expression 9

    s Scriptlet

  • 8/14/2019 jspsyntaxref12

    10/38

    10 JavaServer Pages v1.2 Syntax Reference June 2002

    Scriptlet

    Contains a code fragment valid in the p age scripting language.

    JSP Syntax

    XML Syntax

    code fragment

    Examples

    Description

    A scriptlet can contain any nu mber of language statements, variable or m ethod

    declarations, or expressions that are valid in th e page scripting langua ge.

    Within a scriptlet, you can do any of the following:

    s Declare variables or methods to use later in the JSP page (see also Declaration).

    s Write expressions valid in the page scripting language (see also Expression).

    s Use any of the implicit objects or any object declared with a

    element.

    s Write any other statemen t valid in the scripting language u sed in the JSP pa ge.

  • 8/14/2019 jspsyntaxref12

    11/38

    Scriptlet 11

    Any text, HTML tags, or JSP elements you wr ite must be outside the scriptlet.

    Scriptlets are executed at requ est time, when the JSP container p rocesses the requ est.

    If the scriptlet prod uces outpu t, the outpu t is stored in the out object.

    See Also

    s Declaration

    s Expression

  • 8/14/2019 jspsyntaxref12

    12/38

    12 JavaServer Pages v1.2 Syntax Reference June 2002

    Encloses temp late data.

    JSP Syntax

    None.

    XML Syntax

    template data

    Example

    i=3;

    hi you all

    i

    The output is:

    hi you all

    Description

    A jsp:text element is used to enclose temp late data in th e XML representation . A

    jsp:text element has no attributes and can appear anywhere that template data

    can. The interpretation of a jsp:text element is to pass its content throu gh to th e

    current value ofout.

    XML syntax allows an XML element th at d oes not represent a stand ard or custom

    action to appear an ywh ere a jsp:text can app ear. Such an elem ent is passed to the

    current out.

  • 8/14/2019 jspsyntaxref12

    13/38

    Include Directive 13

    Includ e Directive

    Includes a sta tic file in a JSP p age, p arsing the files JSP elements.

    JSP Syntax

    XML Syntax

    Examples

    include.jsp:

    An Include Test

    The current date and time are

    date.jsp:

    Displays in the page:

    The current date and time are

    Aug 30, 1999 2:38:40

    Description

    An include directive inserts a file of text or code in a JSP page at translation time,

    wh en the JSP pa ge is comp iled. When you u se the includ e directive, the include

    process is static. A static include means th at the text of the includ ed file is add ed to

    the JSP page. The included file can be a JSP page, HTML file, XML document, or text

    file. If the includ ed file is a JSP page, its JSP elemen ts are tr anslated and included

    (along with any other text) in the JSP page. Once the included file is translated and

    includ ed, the translation process resum es w ith the next line of the includ ing JSPpage.

  • 8/14/2019 jspsyntaxref12

    14/38

    14 JavaServer Pages v1.2 Syntax Reference June 2002

    The includ ed file can be a n H TML file, a JSP p age, a text file, XML docum ent, or a

    code file written in the Java programming language. Be careful that the included file

    does n ot contain , , , o r tags. Because the entire

    content of the includ ed file is added to the including JSP p age, these tags wou ld

    conflict with the same tags in the including JSP page, causing an error.

    Some of the behaviors of the includ e d irective depend on the particular JSP

    container you are using, for example:

    s The included file might be open and available to all requests, or it might h ave

    security restrictions.

    s The JSP page might be recompiled if the included file changes.

    Attributes

    s file="relativeURL"

    The path nam e to the included file, which is always a relative URL. A relative URL

    is just th e path segm ent of an URL, w ithou t a protocol, por t, or doma in nam e, like

    this:

    "error.jsp""/templates/onlinestore.html"

    "/beans/calendar.jsp"

    If the relative URL starts w ith / , the p ath is relative to the JSP ap plications

    context, which is a javax.servlet.ServletContext object that is in turn

    stored in the application object. If the relative URL starts w ith a d irectory or

    file name, the path is relative to the JSP page.

    Tip

    If you a re includ ing a text file and do n ot wan t the text to be d isplayed in the JSP

    page, place the text in a comment element.

    See Also

    s

    s

  • 8/14/2019 jspsyntaxref12

    15/38

  • 8/14/2019 jspsyntaxref12

    16/38

  • 8/14/2019 jspsyntaxref12

    17/38

    Page Directive 17

    If the value is false, you cannot use the session object or a

    element with scope=session in the JSP page. Either of these usages would

    cause a translation-time error.

    The default value is true.

    s buffer="none|8kb|sizekb"

    The buffer size in kilobytes used by the out object to handle outp ut sent from th e

    compiled JSP page to the client web browser. The default value is 8kb. If you

    specify a buffer size, the output is buffered with at least the size you specified.

    s autoFlush="true|false"

    Whether the bu ffered outp ut shou ld be flushed autom atically wh en the buffer isfull. If set to true (the default value), the buffer will be flushed. If set to false,

    an exception w ill be raised w hen th e buffer overflows. You cann ot set autoFlush

    to false when buffer is set to none.

    s isThreadSafe="true|false"

    Whether thread safety is implemented in the JSP page. The default value is true,

    wh ich m eans that the JSP container can send mu ltiple, concurrent client requests

    to the JSP page. You m ust w rite code in the JSP page to synchron ize the m ultipleclient threads. If you use false, the JSP container sends client requests one at a

    time to the JSP page.

    s info="text"

    A text string tha t is incorp orated verba tim into th e compiled JSP page. You can

    later retrieve the string w ith the Servlet.getServletInfo()method.

    s errorPage="relativeURL"

    A p athnam e to a JSP pa ge that this JSP pa ge sends exceptions to. If the p athnam e

    begins with a / , the path is relative to the JSP applications document root

    directory and is resolved by the web server. If not, the pathname is relative to the

    current JSP page.

    s isErrorPage="true|false"

    Whether the JSP p age d isplays an error p age. If set to true, you can use the

    exception object in the JSP pag e. If set to false (the default value), you cannot

    use the exception object in the JSP page.

    s contentType="mimeType [; charset=characterSet ]" |

    "text/html;charset=ISO-8859-1"

    The MIME type and character en coding th e JSP pa ge uses for th e respon se. You

    can use any MIME type or character set that are valid for the JSP container. The

    default MIME type is text/html, and th e default character set is ISO-8859-1.

    s pageEncoding="characterSet | ISO-8859-1"

  • 8/14/2019 jspsyntaxref12

    18/38

    18 JavaServer Pages v1.2 Syntax Reference June 2002

    The character encoding the JSP page uses for the response. The default character

    set is ISO-8859-1.

    Tip

    If you need to include a long list of packages or classes in more than one JSP page,

    you can create a separate JSP page with a page directive that contains the import list

    and include that file in the main JSP page.

  • 8/14/2019 jspsyntaxref12

    19/38

    Taglib Directive 19

    Taglib Directive

    Defines a tag library and prefix for the custom tags used in the JSP page.

    JSP Syntax

    XML Syntax

    Non e. How ever, see .

    Examples

    ...

    Description

    The taglib d irective declares that the JSP p age u ses custom tags, nam es the tag

    library that defines them, and specifies their tag prefix.

    You m ust u se a taglib directive before you use th e custom tag in a JSP p age. You can

    use more than one taglib directive in a JSP page, but the prefix defined in each must

    be unique.

    Tutorials on creating custom tags are available at http://java.sun.com/products/jsp/

    taglibraries.html#tutorials .

    Attributes

    s uri="URIForLibrary"

    The Uniform Resource Identifier (URI) that uniquely locates the TLD that

    describes the set of custom tags associated with the named tag prefix. A URI can

    be any of the following:

    s A Uniform Resource Locator (URL), as d efined in RFC 2396, available at

    http://www.hut.fi/u/jkorpela/rfc/2396/full.htmls A Uniform Resource Name (URN), as defined in RFC 2396

    http://java.sun.com/products/jsp/taglibraries.html#tutorialshttp://java.sun.com/products/jsp/taglibraries.html#tutorialshttp://java.sun.com/products/jsp/taglibraries.html#tutorialshttp://java.sun.com/products/jsp/taglibraries.html#tutorials
  • 8/14/2019 jspsyntaxref12

    20/38

    20 JavaServer Pages v1.2 Syntax Reference June 2002

    s An absolute or relative pathnam e

    If the URI is a URL or URN, then the TLD is located by consulting the m app ing

    indicated in web.xml extend ed u sing the imp licit map s in the packaged tag

    libraries. IfURI is pathnam e, it is interpreted relative to the root of the webap plication and shou ld resolve to a TLD file directly, or to a JAR file that h as a

    TLD file at location META-INF/ taglib.tld.

    s prefix="tagPrefix"

    The prefix that p recedes the custom tag nam e, for examp le, public in

    . Empty prefixes are illegal. If you are developing or using

    custom tags, you cannot use th e tag p refixes jsp, jspx, java, javax, servlet,

    sun, and sunw, as they are reserved by Sun Microsystems.

    See Also

    s

  • 8/14/2019 jspsyntaxref12

    21/38

    21

    Forward s a request to a w eb resource.

    JSP Syntax

    or

    +

    XML Syntax

    or

    +

    }

    Examples

    Description

    The element forwards the request object containing the client

    request information from one JSP page to another resource. The target resource can

    be an HTML file, another JSP page, or a servlet, as long as it is in the same

    ap plication context as the forw ard ing JSP page. The lines in th e source JSP page after

    th e element are not p rocessed.

  • 8/14/2019 jspsyntaxref12

    22/38

    22 JavaServer Pages v1.2 Syntax Reference June 2002

    You can p ass param eter nam es and v alues to the target resource by using a

    clause. An examp le of this would be p assing the par ameter nam e

    username (with name="username") and the value scott (with value="scott")

    to a servlet as part of the requ est. If you u se , the target resource

    should be a dynamic resource that can handle the parameters.

    Be careful when using with unbuffered output. If you have used

    the pa ge directive w ith buffer="none" to specify that the outp ut of your JSP page

    should not be buffered, and if the JSP page h as any d ata in the out object, using

    will cause an IllegalStateException.

    Attributess page="{relativeURL | }"

    A String or an expression representing the relative URL of the component to

    wh ich you are forward ing the request. The compon ent can be another JSP page, a

    servlet, or any other object that can respond to a request.

    The relative URL looks like a pathit cannot contain a protocol name, port

    num ber, or d omain n ame. The URL can be absolute or relative to the curren t JSP

    page. If it is absolute (beginning w ith a / ), the path is resolved by your web or

    application server.

    s +

    Sends on e or more nam e/ value p airs as param eters to a dynam ic resource. The

    target resource should be dynamic, that is, a JSP page, servlet, or other resource

    that can process the data that is sent to it as param eters.

    You can u se more than one clause if you need to send more than

    one par ameter to the target resource. The name attribute sp ecifies the p arameter

    nam e and takes a case-sensitive literal string as a value. The value attribute

    specifies the parameter value and takes either a case-sensitive literal string or an

    expression that is evaluated at request time.

    See Alsos Include Directive

    s

    s Page Directive

  • 8/14/2019 jspsyntaxref12

    23/38

    23

    Inserts the value of a bean p roperty into the result.

    JSP Syntax

    XML Syntax

    Examples

    Calendar of

    Description

    The element gets a bean prop erty value using the p ropertys

    getter method s and inserts the value into the response. You m ust create or locate a

    bean with before you use .

    The element has a few limitations you shou ld be aw are of:

    s You can not u se to retrieve the values of an indexed

    property.

    s You can use with JavaBeans compon ents, but not with

    enterprise beans. As alternatives, you can wr ite a JSP p age that retrieves values

    from a bean that in turn r etrieves values from an enterp rise bean, or you can wr ite

    a custom tag that retrieves values from a n enterp rise bean d irectly.

    Attributes

    s name="beanInstanceName"

    The name of an object (usually an instance of a bean) as declared in a

    element.

    s property="propertyName"

  • 8/14/2019 jspsyntaxref12

    24/38

    24 JavaServer Pages v1.2 Syntax Reference June 2002

    The name of the bean prop erty wh ose value you wan t to display. The prop erty is

    declared as a variable in a bean and m ust have a corresponding getter meth od

    (for more information on declaring variables and writing getter methods in beans,

    see http://java.sun.com/products/javabeans/docs/ ).

    Tip

    If you u se to retrieve a property value that is null, a

    NullPointerException is thrown. How ever, if you u se a scriptlet or expression to

    retrieve the value, the string null is displayed in th e browser; see Scriptlet or

    Expression for more information.

    See Also

    s

    s

    http://java.sun.com/products/javabeans/docs/http://java.sun.com/products/javabeans/docs/
  • 8/14/2019 jspsyntaxref12

    25/38

    25

    Includes a static file or the result from an other w eb component.

    JSP Syntax

    or

    +

    XML Syntax

    or

    [ ] + }

    Examples

    Description

    The element allows you to includ e either a static or dynamic

    resource in a JSP pag e. The resu lts of includ ing static and d ynamic resources are quitedifferent. If the r esource is static, its content is includ ed in th e calling JSP p age. If the

  • 8/14/2019 jspsyntaxref12

    26/38

    26 JavaServer Pages v1.2 Syntax Reference June 2002

    resource is dyn amic, it acts on a request and sends back a result that is included in

    the JSP page. When the include action is finished, the JSP container continues

    processing the remaind er of the JSP p age.

    You cann ot always determ ine from a pat hn am e if a resource is static or dy nam ic. Forexample, http://server:8080/index.html might m ap to a servlet through a server alias.

    The element handles both types of resources, so it is convenient to

    use w hen you d ont know w hether the resource is static or dynam ic.

    If the includ ed resource is dynam ic, you can use a clause to p ass the

    nam e and va lue of a para meter to the resource. As an example, you could p ass the

    string username and a user s nam e to a login form tha t is coded in a JSP page.

    Attributes

    s page="{ relativeURL | }"

    The relative URL that locates the resource to be included, or an expression that

    evaluates to a String equivalent to the relative URL.

    The relative URL looks like a path nam eit cannot contain a p rotocol nam e, port

    num ber, or d omain n ame. The URL can be absolute or relative to the curren t JSPpage. If it is absolute (beginning with a / ), the pathname is resolved by your web

    or application server.

    s flush="true | false"

    If the page ou tpu t is buffered and the flush attr ibute is given a tru e value, the

    buffer is flushed prior to the inclusion, otherwise the buffer is not flushed. The

    default value for the flush attribute is false.

    s +

    The clause allows you to p ass one or more nam e/ value pairs as

    param eters to an includ ed resource. The includ ed resource should be d ynam ic,

    that is, a JSP page, servlet, or other resource that can process the parameter.

    You can u se more than one clause if you w ant to send m ore than

    one para meter to the included resource. The name attribute specifies the

    param eter nam e and takes a case-sensitive literal string. The value attribute

    specifies the parameter value and takes either a case-sensitive literal string or an

    expression that is evaluated at request time.

    See Also

    s Include Directive

    s

  • 8/14/2019 jspsyntaxref12

    27/38

    27

    Causes the execution of an ap plet or bean. The app let or bean executes in the

    specified plugin. If the plugin is not available, displays a dialog to initiate the

    download of the plugin software.

    JSP Syntax

    [

    [ ]+

    ]

    [ text message for user ]

    XML Syntax

  • 8/14/2019 jspsyntaxref12

    28/38

  • 8/14/2019 jspsyntaxref12

    29/38

  • 8/14/2019 jspsyntaxref12

    30/38

    30 JavaServer Pages v1.2 Syntax Reference June 2002

    s jreversion="JREVersionNumber|1.2"

    The version of the Java Runtime Environment (JRE) the applet or bean requires.

    The defau lt value is 1.2.

    s nspluginurl="URLToPlugin"

    The URL where the u ser can dow nload th e JRE plug-in for Netscape N avigator.

    The value is a full URL, with a p rotocol name, optional port nu mber, and dom ain

    name.

    s iepluginurl="URLToPlugin"

    The URL where th e user can d ownload the JRE plug-in for Internet Explorer. The

    value is a full URL, with a protocol name, optional port nu mber, and d omainname.

    s

    [ ]+

    The param eters and valu es that you w ant to pa ss to the applet or bean. To specify

    more than one parameter value, you can use more than one element within the element.

    The name attribute specifies the parameter name and takes a case-sensitive literal

    string. The value attribute specifies the param eter value and takes either a case-

    sensitive literal string or an expression that is evaluated at run time.

    If the dyn amic resource you are p assing the p aram eter to is an app let, it reads the

    parameter with the java.applet.Applet.getParametermethod.

    s text message for user

    A text message to display for the u ser if the plug-in cannot be started. If the plug-

    in starts but the app let or bean d oes not, the plug-in usually displays a popu p

    wind ow explaining the error to the user.

    See Also

    s The HTML 3.2 specification: http://www.w3.org/TR/REC-html32.html

    s The HTML 4.0 specification: http://www.w3.org/TR/REC-html40/

    http://www.w3.org/TR/REC-html32.htmlhttp://www.w3.org/TR/REC-html40http://www.w3.org/TR/REC-html40http://www.w3.org/TR/REC-html32.html
  • 8/14/2019 jspsyntaxref12

    31/38

    31

    Sets a property v alue or values in a bean.

    JSP Syntax

    XML Syntax

    Examples

    Description

    The element sets the value of one or more p roperties in a

    bean, using the beans setter method s. You m ust d eclare the bean with before you set a property value with . Becau se

    an d work together, the bean instance names

    they use m ust m atch (that is, the value ofname in and the

    value ofid in mu st be the same).

    You can use to set property values in several ways:

    s By passing all of the values the user enters (stored as p arameters in th e request

    object) to matching properties in the bean

  • 8/14/2019 jspsyntaxref12

    32/38

    32 JavaServer Pages v1.2 Syntax Reference June 2002

    s By passing a specific value the user enters to a specific property in the bean

    s By setting a bean p roperty to a value you specify as either a String or an

    expression that is evaluated at run time

    Each m ethod of setting property values has its own syntax, as described in the nextsection.

    Attributes and Usage

    s name="beanInstanceName"

    The name of an instance of a bean that h as already been created or located w ith a

    element. The valu e ofname must match the value ofid in. The element must appear before in the JSP p age.

    s property="*"

    Stores all of the valu es of requ est param eters in bean p roper ties. The nam es of the

    bean prop erties mu st match the nam es of the request param eters. A bean

    prop erty is usually defined by a va riable declaration w ith matching getter and

    setter method s (for m ore information, see http://java.sun.com/products/javabeans/docs/).

    The values of the request p arameters sent from th e client to the server are always of

    type String. The String values are converted to other d ata types wh en stored in

    bean p roperties. If a prop erty has a PropertyEditor class as indicated in the

    JavaBeans specification, th e setAsText(String) method is used. A conversion

    failure arises if the method throws an IllegalArgumentException. The allowed

    bean property types and their conversion methods are shown in TABLE 1.

    TABLE 1 How Converts Strings to Other Values

    Property Type String Is Converted Using

    Bean Property Use setAsText(stringLiteral)

    boolean or Boolean java.lang.Boolean.valueOf(Str ing)

    byte or Byte java.lan g.Byte.valu eOf(Strin g)

    ch ar o r Ch ar act er ja va .la ng .St rin g.ch ar At (0)

    double o r Double java .lang .Double .valueOf(St ring)

    in teger o r In teger java .lang .In teger.valueOf(St ring)

    float or Floa t jav a.lan g.Floa t.v alu eOf(Str in g)

    http://java.sun.com/products/javabeans/docs/http://java.sun.com/products/javabeans/docs/
  • 8/14/2019 jspsyntaxref12

    33/38

    33

    You can also use to set the value of an indexed prop erty in

    a bean. The indexed prop erty mu st be an array of one of the data types shown in

    TABLE 1. The array elements are converted u sing the conversion method s shown

    in the table.

    If a request param eter has an emp ty or null value, the correspond ing bean

    prop erty is not set. Likewise, if the bean ha s a p roperty that does not h ave a

    matching request pa rameter, the prop erty value is not set.

    s property="propertyName" [ param="parameterName" ]

    Sets one bean p roperty to th e value of one request p aram eter. In the syntax,

    property specifies the nam e of the bean prop erty and param specifies the name

    of the request param eter by w hich data is being sent from the client to the server.

    If the bean prop erty and th e request param eter have different names, you mu st

    specify both property an d param. If they have the same na me, you can sp ecify

    property and omit param.

    If a param eter has an emp ty or null value, the correspond ing bean property is not

    set.

    s property="propertyName" value="{string| }"

    Sets one bean property to a specific value. The value can be a String or an

    expression that is evaluated at ru ntim e. If the valu e is a String, it is converted to

    the bean p ropertys data typ e according to the conversion rules shown above in

    TABLE 1. If it is an expression, its value must have a data type that matches the the

    data type of the value of the expression m ust m atch the data typ e of the bean

    property.

    If the para meter has an em pty or nu ll value, the correspond ing bean property is

    not set. You cann ot use both the param an d value attributes in a element.

    See Also

    s

    s

    lon g or Lon g java.lan g.Lon g.valu eOf(Strin g)

    sh or t or Sh or t ja va .lan g.Sh or t.v alu eO f(Strin g)

    Object new String(string-literal)

    TABLE 1 How Converts Strings to Oth er Values

    Property Type String Is Converted Using

  • 8/14/2019 jspsyntaxref12

    34/38

    34 JavaServer Pages v1.2 Syntax Reference June 2002

    Tip

    When you use property="*", the bean properties are not necessarily set in th e

    order in w hich they appear in the HTML form or th e bean. If the order in wh ich the

    prop erties are set is importan t to how your bean works, use the syntax formproperty="propertyName" [ param="parameterName" ]. Better y et, rewr ite

    your bean so that the order of setting properties is not impor tant.

  • 8/14/2019 jspsyntaxref12

    35/38

    T l t i t ti t th b t k th f ll i t i thi

  • 8/14/2019 jspsyntaxref12

    36/38

    36 JavaServer Pages v1.2 Syntax Reference June 2002

    To locate or instantiate the bean, takes the following steps, in this

    order:

    1. Attempts to locate a bean with the scope and nam e you specify.

    2. Defines an object reference variable with the name you specify.

    3. If it finds th e bean, stores a reference to it in the v ariable. If you specified type,

    gives the bean that type.

    4. If it does n ot find the bean , instan tiates it from the class you sp ecify, storing a

    reference to it in the new variable. If the class name represents a serialized

    template, the bean is instantiated by java.beans.Beans.instantiate.

    5. If has instantiated(rather than located) the bean, and if it has

    body tags or elements (between an d ),

    executes the bod y tags.

    The body of a element often contains a

    element that sets prop erty values in the bean . As described in Step 5, the body tags

    are only processed if instantiates the bean . If the bean already

    exists an d locates it, the body tags have no effect.

    You can u se a element to locate or instantiate a JavaBeans

    component , bu t not an enterprise bean. To create enterprise beans, you can w rite a

    element that calls a bean th at in tu rn calls the enterprise bean, or

    you can write a custom tag that calls an enterprise bean directly.

    Attributes and Usage

    s id="beanInstanceName"

    A var iable that id entifies the bean in the scope you specify. You can u se the

    variable name in expressions or scriptlets in the JSP page.

    The name is case sensitive and mu st conform to the nam ing conventions of the

    scripting language used in the JSP page. If you use the Java program ming

    language, the conventions in the Java Language Specification . If the bean has

    already been created by another element, the value ofid must

    match the value ofid used in the original element.s scope="page|request|session|application"

    The scope in which the bean exists and the variable named in id is available. The

    default value is page. The m eanings of the d ifferent scopes are show n below:

    page You can u se the bean w ithin the JSP pag e with th e

    element or an y of the p ages static include

    files, until the page sends a response back to th e client or

    forward s a request to another resource.

  • 8/14/2019 jspsyntaxref12

    37/38

    The value of type can be the same as beanName a sup erclass of beanName or an

  • 8/14/2019 jspsyntaxref12

    38/38

    38 JavaServer Pages v1.2 Syntax Reference June 2002

    The value oftype can be the same as beanName, a sup erclass ofbeanName, or an

    interface implemented by beanName. The pa ckage and class nam es you u se with

    both beanName an d type are case sensitive.

    See Also

    s

    s

    s http://java.sun.com/products/javabeans/docs/

    http://java.sun.com/products/javabeans/docs/http://java.sun.com/products/javabeans/docs/