background image

Communication between Tags

<< Tags with Bodies | Encapsulating Reusable Content Using Tag Files >>
<< Tags with Bodies | Encapsulating Reusable Content Using Tag Files >>

Communication between Tags

Communication between Tags
Custom tags communicate with each other through shared objects. There are two types of
shared objects: public and private.
In the following example, the c:set tag creates a public EL variable called aVariable, which is
then reused by anotherTag.
<c:set var=
"aVariable" value="aValue" />
<tt:anotherTag attr1=
"${aVariable}" />
Nested tags can share private objects. In the next example, an object created by outerTag is
available to innerTag. The inner tag retrieves its parent tag and then retrieves an object from
the parent. Because the object is not named, the potential for naming conflicts is reduced.
<tt:outerTag>
<tt:innerTag />
</tt:outerTag>
The Duke's Bookstore page
tut-install/javaeetutorial5/examples/web/bookstore3/web/template/template.jsp uses
a set of cooperating tags that share public and private objects to define the screens of the
application. These tags are described in
"A Template Tag Library" on page 267
.
Encapsulating Reusable Content Using Tag Files
A tag file is a source file that contains a fragment of JSP code that is reusable as a custom tag. Tag
files allow you to create custom tags using JSP syntax. Just as a JSP page gets translated into a
servlet class and then compiled, a tag file gets translated into a tag handler and then compiled.
The recommended file extension for a tag file is .tag. As is the case with JSP files, the tag can be
composed of a top file that includes other files that contain either a complete tag or a fragment
of a tag file. Just as the recommended extension for a fragment of a JSP file is .jspf, the
recommended extension for a fragment of a tag file is .tagf.
The following version of the Hello, World application introduced in
Chapter 3, "Getting Started
with Web Applications"
uses a tag to generate the response. The response tag, which accepts
two attributes (a greeting string and a name) is encapsulated in response.tag:
<%@ attribute name=
"greeting" required="true" %>
<%@ attribute name=
"name" required="true" %>
<h2><font color=
"black">${greeting}, ${name}!</font></h2>
The highlighted line in the greeting.jsp page invokes the response tag if the length of the
username
request parameter is greater than 0:
Encapsulating Reusable Content Using Tag Files
Chapter 8 · Custom Tags in JSP Pages
233