10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

ProgrammingTagsThat Accept Scripting Elements<br />

282<br />

TagsThat DefineVariables<br />

<strong>The</strong> mechanisms for defining variables in classic tags are similar to those described in<br />

Chapter 8, “Custom Tags in JSP Pages.” You must declare the variable in a variable element of<br />

the TLD or in a tag extra info class. Use PageContext().setAttribute(name,value) or<br />

PageContext.setAttribute(name,value,scope) methods in the tag handler to create or<br />

update an association between a name that is accessible in the page context and the object that is<br />

the value of the variable. For classic tag handlers, Table 9–3 illustrates how the availability of a<br />

variable affects when you may want to set or update the variable’s value.<br />

TABLE 9–3 Variable Availability<br />

Value Availability In Methods<br />

NESTED Between the start tag and the end tag doStartTag, doInitBody, and doAfterBody<br />

AT_BEGIN From the start tag until the end of the page doStartTag, doInitBody, doAfterBody, and<br />

doEndTag<br />

AT_END After the end tag until the end of the page doEndTag<br />

A variable defined by a custom tag can also be accessed in a scripting expression. For example,<br />

the web service described in the preceding section can be encapsulated in a custom tag that<br />

returns the response in a variable named by the var attribute, and then var can be accessed in a<br />

scripting expression as follows:<br />

<br />

!<br />

Remember that in situations where scripting is not allowed (in a tag body where the<br />

body-content is declared as scriptless and in a page where scripting is specified to be<br />

invalid), you wouldn’t be able to access the variable in a scriptlet or an expression. Instead, you<br />

would have to use the JSP expression language to access the variable.<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!