BodyTagSupport Example

BodyTagSupport class implements the BodyTag interface and you can add additional functionalities in it to set property of bodyContent.

BodyTagSupport Example

Example to illustrate the use of BodyTagSupport in a JSP page

BodyTagSupport class implements the BodyTag interface and you
can add additional functionalities in it to set property of bodyContent.
If you want to add other functionalities in this tag then there are some methods
which you have to override. Some important methods of this class are as
follows:

doAfterBody() -After the body evaluation it does not
reevaluate and continue with the page

This example will illustrate you how to make custom tags in JSP along with
implementation of BodyTagSupport's methods. To make custom tags in JSP
and to use them, we have made an application which have <tbl:stag>
</tbl:stag> tag which is being used to convert all contents of body of
the tag to
Upper case. For example, if it is written like this in JSP page

<tbl:stag>
hello ! i am Amit Kumar Raghuwanshi
</tbl:stag>

So when JSP page would be executed then this tag will convert the body
content "hello ! i am Amit Kumar Raghuwanshi" to the following source
code in HTML like this:

<body>
HELLO ! I AM AMIT KUMAR RAGHUWANSHI
</body>

To implement these functionalities we have created following files:

MyTag.java

BodyTagExample.jsp

stag.tld

MyTag.java is a simple java file which is extending BodyTagSupport
class. In this file we have overridden only one method of BodyTagSupport, doAfterBody(), which is firstly taking the bodyContent to a string
named "body" and then bodycontent.getEnclosingWriter() is
returning object of JspWriter type and these contents are to be converted
in UpperCase and then it will be displayed on the browser. This method doAfterBody()
is returning SKIP_BODY means that after the body evaluation :do not
reevaluate and continue with the page. Full code of this MyTag.java is
given as below :

Another very important file is stag.tld which is playing very
important role in this example. This "stag.tld" is an XML file
which is setting the shortname, name, bodycontent ,tagclass
of this tag. Using these parameters setting in this tld file we are going
to make use of it in our JSP page in the following manner: