The Tag Handler Class
Up to this point,
all of the tag handlers have extended the TagSupport class.
This is a good standard starting point, as it implements the required Tag interface
and performs a number of useful setup operations like storing the PageContext reference
in the pageContext field.
However, TagSupport is
not powerful enough for tag implementations that need to manipulate their body
content, and BodyTagSupport should be used
instead.
BodyTagSupport extends
TagSupport,
so the doStartTag and
doEndTag methods
are used in the same way as before. The two important new methods defined by BodyTagSupport are:
1. doAfterBody, a method that you should
override to handle the manipulation of the tag body. This method should
normally return SKIP_BODY when it is done, indicating
that no further body processing should be performed.
2.
getBodyContent, a method that
returns an object of type BodyContent that encapsulates information
about the tag body.
The
BodyContent class
has three important methods:
1.
getEnclosingWriter, a method
that returns the JspWriter being used by doStartTag
and doEndTag.
2.
getReader, a method that
returns a Reader that
can read the tag’s body.
3.
getString, a method that
returns a String containing
the entire tag body.
The
tag handler includes the following classes and methods:
__
The JspWriter() method has to
be explicitly mentioned in the tag to write the output to a JSP page.
__ The
getAttribute() and
setAttribute() methods
are used to retrieve variable values from scriplets. After processing the
variable, its value is then set using the setAttribute() method.
When using the getAttribute() and
setAttribute() methods,
the details about the scripting variable also need to be specified using the TagExtraInfo
class.
__ The
TagExtraInfo class
uses the getVariableInfo() method
to return information about the scripting values retrieved from the scriplets TagExtraInfo()
method, which is the default constructor for this class, and
the
setTagInfo() and
getTagInfo() methods
to set and get the TagInfo object for this
class.
The
steps to execute the JSP file areas follows:
1. When the JSP
engine comes across the taglib directive
in a JSP page, it recognizes the presence of a custom tag associated with the
JSP file. The uri and prefix attributes for a tag act as
referential data for specifying the unique URI and name for the tag.
2.
The specified tag handler is initialized.
3.
The getXXX() and setXXX() methods
for each of the attributes are then executed.
4.
The doStartTag() method
is invoked and used to perform tasks such as establishing a connection with the
database.
5.
The tag body is evaluated next but is skipped if the SKIP_BODY field constant is specified. In that case,
the method invoked is doEndTag().
6. The setBodyContent() method is then invoked to store the output
of the tag in a special print writer called JspWriter. The pageContext.get.Out() is used for this so that the contents are
available for the subsequent methods. The output is not forwarded to the client
at this stage.
7. Next, the doAfter() method is invoked to work on the content
generated after the evaluation of the tag body. The field constants SKIP_BODY and EVAL_BODY_TAG field
constants can be returned to estimate the exact stage of the life cycle.
8. The doEndTag() method is invoked next. All connections
established earlier are closed, and the output is directed to the browser.
The
tag handler file for the simple tag will consist of the following statements
and methods:
1.
The import statements for
importing all relevant packages
2.
Inclusion of the interface that has to be extended
3.
The doStartTag () method
to initialize the tag handler
4.
The doEndTag() containing the
message string and the method to extract the current date and time










No comments:
Post a Comment