{"id":1724,"date":"2011-03-17T17:56:09","date_gmt":"2011-03-17T12:26:09","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1724"},"modified":"2011-03-17T17:56:09","modified_gmt":"2011-03-17T12:26:09","slug":"how-to-create-jsp-custom-tag-using-bodytag-interface-or-bodytagsupport","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/how-to-create-jsp-custom-tag-using-bodytag-interface-or-bodytagsupport\/","title":{"rendered":"How to Create JSP Custom Tag \u2013 using BodyTag interface or BodyTagSupport"},"content":{"rendered":"<p>In Previous articles, i have explained:<\/p>\n<p><a title=\"Life cycle of JSP tag interface\" href=\"https:\/\/jitendrazaa.com\/blog\/java\/life-cycle-of-jsp-tag-interface\/\" target=\"_blank\">Life Cycle of JSP &#8220;Tag&#8221; interface.<\/a><\/p>\n<p><a title=\"Life cycle of jsp BodyTag interface\" href=\"https:\/\/jitendrazaa.com\/blog\/java\/life-cycle-of-jsp-bodytag-interface\/\" target=\"_blank\">Life Cycle of JSP &#8220;BodyTag&#8221; interface<\/a>.<\/p>\n<p><a title=\"Create Custom Tag using Tag Interface\" href=\"https:\/\/jitendrazaa.com\/blog\/java\/how-to-create-jsp-custom-tag-%E2%80%93-using-tag-interface-or-tagsupport\/\" target=\"_blank\">How to Create JSP Custom Tag \u2013 using Tag interface or TagSupport<\/a><\/p>\n<p>we have seen that how to create the custom JSP tag using interface &#8220;Tag&#8221;\u009d, means tags which does not have a body. In this article, I will explain creating custom Tags which have a body.\u00a0We can manipulate the content of body as we want.<\/p>\n<figure id=\"attachment_1721\" aria-describedby=\"caption-attachment-1721\" style=\"width: 470px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/How-to-Create-Custom-Tag-in-JSP.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1721\" title=\"How to Create Custom Tag in JSP\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/How-to-Create-Custom-Tag-in-JSP.jpg?resize=470%2C609&#038;ssl=1\" alt=\"How to Create Custom Tag in JSP\" width=\"470\" height=\"609\" \/><\/a><figcaption id=\"caption-attachment-1721\" class=\"wp-caption-text\">How to Create Custom Tag in JSP<\/figcaption><\/figure>\n<p><!--more--><\/p>\n<p>So, create a class which implements interface &#8220;<strong>BodyTag<\/strong>&#8220;\u009d, in this article also I am not going to use Support classes. As it will not clear the basic idea.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.G2.CustomTag;\n\nimport java.io.IOException;\nimport java.text.SimpleDateFormat;\nimport java.util.Date;\n\nimport javax.servlet.jsp.JspException;\nimport javax.servlet.jsp.JspWriter;\nimport javax.servlet.jsp.PageContext;\nimport javax.servlet.jsp.tagext.BodyContent;\nimport javax.servlet.jsp.tagext.BodyTag;\nimport javax.servlet.jsp.tagext.Tag;\n\npublic class DisplayDateBody implements BodyTag {\n\tprivate PageContext pc = null;\n\tprivate Tag parent = null;\n\tprivate BodyContent bd = null;\n\tprivate boolean showInUpperCase;\n\n\tpublic boolean isShowInUpperCase() {\n\t\treturn showInUpperCase;\n\t}\n\n\tpublic void setShowInUpperCase(boolean showInUpperCase) {\n\t\tthis.showInUpperCase = showInUpperCase;\n\t}\n\n\t@Override\n\tpublic void doInitBody() throws JspException {\n\n\t}\n\n\t@Override\n\tpublic void setBodyContent(BodyContent arg0) {\n\t\tbd = arg0;\n\n\t}\n\n\t@Override\n\tpublic int doAfterBody() {\n\t\ttry {\n\t\t\tString bodyString = bd.getString();\n\t\t\tJspWriter out = bd.getEnclosingWriter();\n\t\t\tSimpleDateFormat frm = new SimpleDateFormat(&quot;dd-MMM-yyy EEEE&quot;);\n\t\t\tif(showInUpperCase)\n\t\t\t{\n\t\t\t\tout.print((bodyString + frm.format(new Date())).toUpperCase());\n\t\t\t}else{\n\t\t\t\tout.print((bodyString + frm.format(new Date())).toLowerCase());\n\t\t\t}\n\t\t\tbd.clear(); \/\/ empty buffer for next evaluation\n\t\t} catch (IOException e) {\n\t\t\tSystem.out.println(&quot;Error in BodyContentTag.doAfterBody()&quot; + e.getMessage());\n\t\t\te.printStackTrace();\n\t\t} \/\/ end of catch\n\n\t\treturn SKIP_BODY;\n\t}\n\n\t@Override\n\tpublic int doEndTag() throws JspException {\n\t\treturn EVAL_PAGE;\n\t}\n\n\t@Override\n\tpublic int doStartTag() throws JspException {\n\t\treturn EVAL_BODY_AGAIN;\n\t}\n\n\t@Override\n\tpublic Tag getParent() {\n\t\treturn parent;\n\t}\n\n\t@Override\n\tpublic void release() {\n\t\tpc = null;\n\t\tparent = null;\n\n\t}\n\n\t@Override\n\tpublic void setPageContext(PageContext arg0) {\n\t\tpc = arg0;\n\t}\n\n\t@Override\n\tpublic void setParent(Tag arg0) {\n\t\tparent = arg0;\n\t}\n\n}\n<\/pre>\n<p>As per discussed in life cycle of BodtTag interface, two extra method is added to evaluate the body content of the tag.<br \/>\nNow create  tld file in &#8220;WEB-INF\/tlds&#8221;\u009d folder as shown below:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&lt;!DOCTYPE taglib PUBLIC &quot;-\/\/Sun Microsystems, Inc.\/\/DTD JSP Tag Library 1.1\/\/EN&quot;\n&quot;http:\/\/java.sun.com\/j2ee\/dtds\/web-jsptaglibrary_1_1.dtd&quot;&gt;\n&lt;taglib&gt;\n\t&lt;tlibversion&gt;1.0.0&lt;\/tlibversion&gt;\n\t&lt;jspversion&gt;1.1&lt;\/jspversion&gt;\n\t&lt;shortname&gt;CustomTag&lt;\/shortname&gt;\n\t&lt;uri&gt;https:\/\/jitendrazaa.com\/blog&lt;\/uri&gt;\n\t&lt;info&gt;Custom tags&lt;\/info&gt;\n\n\t&lt;tag&gt;\n\t\t&lt;name&gt;ShowDateWithCase&lt;\/name&gt;\n\t\t&lt;tagclass&gt;com.G2.CustomTag.DisplayDateBody&lt;\/tagclass&gt;\n\t\t&lt;info&gt;Custom tag used to display the current date with option to choose uppercase&lt;\/info&gt;\n\t\t&lt;attribute&gt;\n\t\t\t&lt;name&gt;showInUpperCase&lt;\/name&gt;\n\t\t\t&lt;required&gt;true&lt;\/required&gt;\n\t\t&lt;\/attribute&gt;\n\t&lt;\/tag&gt;\n\n&lt;\/taglib&gt;\n<\/pre>\n<p>Note here that attribute  <em>empty<\/em> used in previous article is not used here, as we have bodycontent here, so it cannot be left blank.<\/p>\n<p>Now create JSP page:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;%@ page language=&quot;java&quot; contentType=&quot;text\/html; charset=ISO-8859-1&quot;\n\tpageEncoding=&quot;ISO-8859-1&quot;%&gt;\n&lt;%@taglib uri=&quot;\/WEB-INF\/tlds\/CustomTags.tld&quot; prefix=&quot;ct&quot;%&gt;\n&lt;!DOCTYPE html PUBLIC &quot;-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN&quot; &quot;http:\/\/www.w3.org\/TR\/html4\/loose.dtd&quot;&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text\/html; charset=ISO-8859-1&quot;&gt;\n&lt;title&gt;Custom Tag&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;h2&gt;Custom tag to display date&lt;\/h2&gt;\n&lt;ct:ShowDateWithCase showInUpperCase=&quot;true&quot;&gt;\nCurrent Date is\n&lt;\/ct:ShowDateWithCase&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n<p>We can also add the<strong> loop functionality<\/strong> in our code, what we have to do additionally is:<\/p>\n<ol>\n<li>Add one more attribute in tag library descriptor (tld) file.<\/li>\n<li>Create getter and setter for that attribute in Tag handler class.<\/li>\n<li>Add one more variable counter in tag handler class.<\/li>\n<li>initialize that variable in method doStartTag(), doInitBody() or any other setter of attribute.<\/li>\n<li>in method doAfterBody(), check the condition if counter&lt;loopcounter then return &#8220;EVAL_BODY_BUFFERED&#8221; else return &#8220;SKIP_BODY&#8221;. \u00a0EVAL_BODY_BUFFERED will tell the page compiler that eval that body again until the &#8220;SKIP_BODY&#8221; returned by the method.<\/li>\n<\/ol>\n<p>Please leave your comment for any suggestion or queries.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tutorial on How to Create JSP Custom Tag \u2013 using BodyTag interface or BodyTagSupport<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,4],"tags":[330],"class_list":["post-1724","post","type-post","status-publish","format-standard","hentry","category-java","category-jsp","tag-jsp"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1716,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/jsp\/life-cycle-of-jsp-bodytag-interface\/","url_meta":{"origin":1724,"position":0},"title":"Life cycle of JSP BodyTag interface","author":"Jitendra","date":"March 17, 2011","format":false,"excerpt":"Life cycle of JSP BodyTag interface","rel":"","context":"In &quot;JSP&quot;","block_context":{"text":"JSP","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/jsp\/"},"img":{"alt_text":"Life cycle of IterationTag and BodyTag interface in JSP","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Life-cycle-of-IterationTag-and-BodyTag-interface-in-JSP.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1720,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/how-to-create-jsp-custom-tag-using-tag-interface-or-tagsupport\/","url_meta":{"origin":1724,"position":1},"title":"How to Create JSP Custom Tag \u2013 using Tag interface or TagSupport","author":"Jitendra","date":"March 17, 2011","format":false,"excerpt":"Tutorial of creating JSP Custom Tag \u2013 using Tag interface or TagSupport in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"How to Create Custom Tag in JSP","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/How-to-Create-Custom-Tag-in-JSP.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1712,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/jsp\/life-cycle-of-jsp-tag-interface\/","url_meta":{"origin":1724,"position":2},"title":"Life cycle of JSP Tag interface","author":"Jitendra","date":"March 17, 2011","format":false,"excerpt":"Life cycle of JSP Tag interface in Java","rel":"","context":"In &quot;JSP&quot;","block_context":{"text":"JSP","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/jsp\/"},"img":{"alt_text":"Life Cycle of Tag Interface","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Life-Cycle-of-Tag-Interface.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1707,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/jsp\/setup-application-to-use-javaserver-pages-standard-tag-libraryjstl\/","url_meta":{"origin":1724,"position":3},"title":"Setup application to use JavaServer Pages Standard Tag Library(JSTL)","author":"Jitendra","date":"March 17, 2011","format":false,"excerpt":"Setup application to use JavaServer Pages Standard Tag Library(JSTL)","rel":"","context":"In &quot;JSP&quot;","block_context":{"text":"JSP","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/jsp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2034,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/jsp\/tutorial-of-simple-jsp-tiles-application-without-struts\/","url_meta":{"origin":1724,"position":4},"title":"Tutorial of Simple JSP Tiles application without Struts","author":"Jitendra","date":"April 15, 2011","format":false,"excerpt":"Example of Simple JSP Tiles application without Struts","rel":"","context":"In &quot;JSP&quot;","block_context":{"text":"JSP","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/jsp\/"},"img":{"alt_text":"Simple JSP Tiles without Struts","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/Simple-JSP-Tiles-without-Struts.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1460,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/jsp\/setting-session-timeout-in-jsp-servlet\/","url_meta":{"origin":1724,"position":5},"title":"Setting Session Timeout in JSP Servlet","author":"Jitendra","date":"February 7, 2011","format":false,"excerpt":"How to set the Session Time out in JSP and Servlet","rel":"","context":"In &quot;JSP&quot;","block_context":{"text":"JSP","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/jsp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1724","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=1724"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1724\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}