{"id":1720,"date":"2011-03-17T17:48:51","date_gmt":"2011-03-17T12:18:51","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1720"},"modified":"2011-03-17T17:48:51","modified_gmt":"2011-03-17T12:18:51","slug":"how-to-create-jsp-custom-tag-using-tag-interface-or-tagsupport","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/how-to-create-jsp-custom-tag-using-tag-interface-or-tagsupport\/","title":{"rendered":"How to Create JSP Custom Tag \u2013 using Tag interface or TagSupport"},"content":{"rendered":"<p>In Previous two 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>This article is about creating custom Tag in JSP using &#8220;Tag&#8221; interface.<br \/>\nCreating custom tag in JSP has its unique advantages. This is same as Custom controls of the ASP.Net.<br \/>\nBy creating the Custom tag, we can resuse that tag every where in our application. Best example of custom tags are the Struts.<\/p>\n<p><strong>Types of Tag:<\/strong><br \/>\nTag without body:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;ct:showTime displayTime=\"\u009dfalse\"\u009d \/&gt;\n<\/pre>\n<p>Tag with body:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;ct:showTime displayTime=\"\u009dfalse\"\u009d &gt;\nCurrent Time is :\n&lt;\/ct:showTime&gt;\n<\/pre>\n<p>As ther are two types of tag, there are two way of creating them in JSP:<\/p>\n<ul>\n<li>By implementing below interfaces:\n<ol>\n<li>Tag<\/li>\n<li>BodyTag<\/li>\n<\/ol>\n<\/li>\n<li>Or by extending any of the two below class (provides default implementation of interfaces discussed above):\n<ol>\n<li>TagSupport (internally implements Tag)<\/li>\n<li>BodyTagSupport (internally implements BodyTag)<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<p>In this article, I will use the <strong>interface approach<\/strong>, so that the beginners can get broader idea about how Tag library actually implemented.<\/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--><br \/>\n<strong>Step 1: Write Tag Handler Class<\/strong><\/p>\n<p><strong><\/strong>Create a class which implements interface Tag.<\/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.Tag;\n\npublic class DisplayDate implements Tag {\n\n\tprivate PageContext pc = null;\n\tprivate Tag parent = null;\n\tprivate boolean displayDate;\n\n\tpublic boolean isDisplayDate() {\n\t\treturn displayDate;\n\t}\n\n\tpublic void setDisplayDate(boolean displayDate) {\n\t\tthis.displayDate = displayDate;\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\ttry {\n\t\t\tJspWriter out = pc.getOut();\n\t\t\tif (isDisplayDate()) {\n\t\t\t\tSimpleDateFormat frm = new SimpleDateFormat(&quot;dd-MMM-yyy EEEE&quot;);\n\t\t\t\tout.print(frm.format(new Date()));\n\t\t\t} else {\n\t\t\t\tout.print(&quot;Custom Tag is not configured to display Date&quot;);\n\t\t\t}\n\t\t} catch (IOException e) {\n\t\t\tthrow new JspException(e);\n\t\t}\n\t\treturn SKIP_BODY;\n\t}\n\n\t@Override\n\tpublic Tag getParent() {\n\n\t\treturn parent;\n\t}\n\n\t@Override\n\tpublic void release() {\n\t\tpc = null;\n\t\tparent = null;\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>In above class, you can see that I have created two objects of <strong>Tag<\/strong> and <strong>PageContext<\/strong>. getParent() and setParent() method uses the object of Tag to specify that who is the parent of that custom Tag. setPageContext() is used to set the PageContext for out custom Tag.<br \/>\nTag Attributes must have setters and getters, in above class displayDate is the attribute.<br \/>\n<strong>doStartTag()<\/strong>, this is the method where we have to write our code. If the attributes value is true then it displays the date other wise displays the message.<br \/>\n<strong>release()<\/strong>, this method is responsible to clear all the resources used by this custom tag. To know more about the life cycle of custom tag using Tag interface, please read this article.<\/p>\n<p><strong>Step 2 : create Tag Library Descriptor (TLD) File<\/strong><\/p>\n<p>Now create xml file named &#8220;CustomTags.tld&#8221;\u009d in folder &#8220;<strong>WEB-INF\/tlds<\/strong>&#8220;\u009d and below code lines:<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>We can also make Tag Handler class and tld file\u00a0accessible\u00a0by creating a jar and placing in lib folder.<\/strong><\/span><\/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\t&lt;tag&gt;\n\t\t&lt;name&gt;ShowDate&lt;\/name&gt;\n\t\t&lt;tagclass&gt;com.G2.CustomTag.DisplayDate&lt;\/tagclass&gt;\n\t\t&lt;bodycontent&gt;empty&lt;\/bodycontent&gt;\n\t\t&lt;info&gt;Custom tag used to display the current date&lt;\/info&gt;\n\t\t&lt;attribute&gt;\n\t\t\t&lt;name&gt;displayDate&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&lt;\/taglib&gt;\n\n<\/pre>\n<p><strong>tlibversion <\/strong>\u2013 Your tag library version<br \/>\n<strong>jspversion <\/strong>\u2013 jsp version<br \/>\n<strong>shortname <\/strong>&#8211; Refers to the name of the JSP Custom Tag that your jsp code will be referring to<br \/>\n<strong>name <\/strong>\u2013 Refers to the name the Custom Tags<br \/>\n<strong>tagclass <\/strong>\u2013 Refersn to the class structure used for your JSP Tags<br \/>\n<strong>bodycontent <\/strong>\u2013 This is used in case we are using BodyTagSupport, in TagSupport we keep this empty.<\/p>\n<p>Now create &#8220;index.jsp&#8221;\u009d with following lines of code:<\/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:ShowDate displayDate=&quot;true&quot; \/&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n<figure id=\"attachment_1722\" aria-describedby=\"caption-attachment-1722\" style=\"width: 280px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/output-of-the-custom-tag-created-using-Tag-interface.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1722\" title=\"output of the custom tag created using Tag interface\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/output-of-the-custom-tag-created-using-Tag-interface.jpg?resize=280%2C151&#038;ssl=1\" alt=\"output of the custom tag created using Tag interface\" width=\"280\" height=\"151\" \/><\/a><figcaption id=\"caption-attachment-1722\" class=\"wp-caption-text\">output of the custom tag created using Tag interface<\/figcaption><\/figure>\n<p>Next article : <a title=\"Create Custom tag using BodyTag interface\" href=\"https:\/\/jitendrazaa.com\/blog\/java\/how-to-create-jsp-custom-tag-%e2%80%93-using-bodytag-interface-or-bodytagsupport\/\" target=\"_blank\">Create Custom tag using BodyTag interface<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tutorial of creating JSP Custom Tag \u2013 using Tag interface or TagSupport in JAVA<\/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-1720","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":1724,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/how-to-create-jsp-custom-tag-using-bodytag-interface-or-bodytagsupport\/","url_meta":{"origin":1720,"position":0},"title":"How to Create JSP Custom Tag \u2013 using BodyTag interface or BodyTagSupport","author":"Jitendra","date":"March 17, 2011","format":false,"excerpt":"Tutorial on How to Create JSP Custom Tag \u2013 using BodyTag interface or BodyTagSupport","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":1716,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/jsp\/life-cycle-of-jsp-bodytag-interface\/","url_meta":{"origin":1720,"position":1},"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":1712,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/jsp\/life-cycle-of-jsp-tag-interface\/","url_meta":{"origin":1720,"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":1720,"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":1460,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/jsp\/setting-session-timeout-in-jsp-servlet\/","url_meta":{"origin":1720,"position":4},"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":[]},{"id":2034,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/jsp\/tutorial-of-simple-jsp-tiles-application-without-struts\/","url_meta":{"origin":1720,"position":5},"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":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1720","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=1720"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1720\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}