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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Programming SimpleTag Handlers<br />

254<br />

servlet-specific behavior. Through these objects, a tag handler can retrieve all the other implicit<br />

objects (request, session, and application) that are accessible from a JSP page. If the tag is nested,<br />

a tag handler also has access to the handler (called the parent) that is associated with the<br />

enclosing tag.<br />

IncludingTag Handlers inWeb Applications<br />

Tag handlers can be made available to a web application in two basic ways. <strong>The</strong> classes<br />

implementing the tag handlers can be stored in an unpacked form in the /WEB-INF/classes/<br />

subdirectory of the web application. Alternatively, if the library is distributed as a JAR, it is<br />

stored in the /WEB-INF/lib/ directory of the web application.<br />

How Is a SimpleTag Handler Invoked?<br />

<strong>The</strong> SimpleTag interface defines the basic protocol between a simple tag handler and a JSP<br />

page’s servlet. <strong>The</strong> JSP page’s servlet invokes the setJspContext, setParent, and attribute<br />

setting methods before calling doStartTag.<br />

ATag t = new ATag();<br />

t.setJSPContext(...);<br />

t.setParent(...);<br />

t.setAttribute1(value1);<br />

t.setAttribute2(value2);<br />

...<br />

t.setJspBody(new JspFragment(...))<br />

t.doTag();<br />

<strong>The</strong> following sections describe the methods that you need to develop for each type of tag<br />

introduced in “Types of Tags” on page 227.<br />

Tag Handlers for BasicTags<br />

<strong>The</strong> handler for a basic tag without a body must implement the doTag method of the SimpleTag<br />

interface. <strong>The</strong> doTag method is invoked when the end element of the tag is encountered.<br />

<strong>The</strong> basic tag discussed in the first section, , would be implemented by the<br />

following tag handler:<br />

public HelloWorldSimpleTag extends SimpleTagSupport {<br />

public void doTag() throws JspException, IOException {<br />

getJspContext().getOut().write("Hello, world.");<br />

}<br />

}<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!