{"id":168,"date":"2018-07-11T10:48:43","date_gmt":"2018-07-11T10:48:43","guid":{"rendered":"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=168"},"modified":"2019-05-15T07:38:41","modified_gmt":"2019-05-15T07:38:41","slug":"deploying-and-using-custom-tags","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/chapter\/deploying-and-using-custom-tags\/","title":{"rendered":"Deploying and Using Custom Tags"},"content":{"raw":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/YZCuWHxUSb4\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Introduction<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">In the last module, we have seen what is a custom tag, how do we develop a custom tag, we have seen that there were three kinds of custom tags, we have a simple tag, we had a iteration tag and we also have a body tag.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">In this module we just want to see, how do we develop this kind of tags and what is required for deploying them in a web application. Towards the end in the last module we had also seen that we should be creating some kind of a TLD file. We will see, how do we create a TLD file what kind of content goes in a TLD file, and we will also develop a couple of tags.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">We will be developing the tag for getting the current date and time in a JSP. So, in any JSP if I want current date and time, I should be able to show the current date and time of the server, and then we might require formatting, so, we will see how we can do that. Another tag which will be used as a iteration tag., we can have some kind of a count for a ForTag. So, we will be creating two tags.<\/p>\r\n&nbsp;\r\n\r\nNowTag and\r\n\r\nForTag\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The idea is to demonstrate, how do we develop custom tag, and use it in a web application, what is required for deploying it in a web application<\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\"><strong>Demo Of creating Custom Tags\u00a0<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">In this demo we will develop two custom tags, one is a simple tag, which can be used for showing the current date and time of the server and another tag is a iteration tag, which can be used to loop a number of times specified by a count attribute. We will than create a tld file so that these tags can be used in a JSP page, and then we also develop a small JSP file to show how the tags can be included in a JSP and used.<\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\"><strong>Developing the NowTag class\u00a0<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The NowTag class is an implementation of the Tag interface. It has an attribute for the formatting the date and time. This is developed as an empty tag, and the doEndTag method is used for generating the otuput according the format attribute.<\/p>\r\n&nbsp;\r\n\r\nThe code for NowTag class is as follows:\r\n\r\nCode for NowTag class\r\n\r\n&nbsp;\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">package org.epgpathshala.dad.tag;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">import javax.servlet.*;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">import javax.servlet.jsp.*;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">import javax.servlet.jsp.tagext.*;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">import java.io.IOException;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">import java.util.Date;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">import java.text.SimpleDateFormat;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">public class NowTag implements Tag { private PageContext pageContext; private Tag parent;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">private String format = \"dd\/MM\/yyyy\";<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\npublic void setPageContext(PageContext pc) { this.pageContext = pc;\r\n\r\n}\r\n\r\npublic void setParent(Tag p) { this.parent = p;\r\n\r\n}\r\n\r\npublic Tag getParent() {\r\n\r\nreturn this.parent;\r\n\r\n}\r\n\r\npublic void setFormat(String fmt) { this.format = fmt;\r\n\r\n}\r\n\r\npublic int doStartTag() {\r\n\r\nreturn Tag.SKIP_BODY;\r\n\r\n}\r\n\r\npublic int doEndTag() { Date d = new Date();\r\n\r\nSimpleDateFormat sdf = new SimpleDateFormat(format); try {\r\n\r\npageContext.getOut().println(sdf.format(d));\r\n\r\n} catch(IOException ioe) {\r\n\r\nioe.printStackTrace();\r\n\r\n}\r\n\r\nreturn Tag.EVAL_PAGE;\r\n\r\n}\r\n\r\npublic void release() {\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n}\r\n\r\n<\/div>\r\n<strong style=\"text-align: initial; font-size: 1em;\">Developing the For Tag class\u00a0<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">The ForTag class is an implementation of the IterationTag interface. It has an attribute for the number of times the loop should be done. This is developed to have a body, and the doAfterBody method decrements the count and checks for zero.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">The code for ForTag class is as follows: Code for ForTag class<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">package org.epgpathshala.dad.tag;<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">import javax.servlet.*;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">import javax.servlet.jsp.*;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">import javax.servlet.jsp.tagext.*;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">import java.io.IOException;<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public class ForTag implements IterationTag { private PageContext pageContext;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">private Tag parent;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">private int count;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void setPageContext(PageContext ctx) { this.pageContext = ctx;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void setParent(Tag p) {<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">this.parent = p;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public Tag getParent() { return this.parent;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void setCount(int c) {<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">this.count = c;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public int doStartTag() {<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">return count&lt;=0?Tag.SKIP_BODY:Tag.EVAL_BODY_INCLUDE;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public int doAfterBody() {<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">count--;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">return count&lt;=0?Tag.SKIP_BODY:IterationTag.EVAL_BODY_AGAIN;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public int doEndTag() {<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">return Tag.EVAL_PAGE;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void release() {<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\"><span style=\"text-align: initial; font-size: 1em;\">TLD file\u00a0<\/span><\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">The tld file for using the two tags is as follows: Code for simple.tld<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;taglib&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;tlib-version&gt;1.0&lt;\/tlib-version&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;jsp-version&gt;1.2&lt;\/jsp-version&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;short-name&gt;Simple&lt;\/short-name&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;tag&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;name&gt;Now&lt;\/name&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;tag-class&gt;org.epgpathshala.dad.NowTag&lt;\/tag-class&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;body-content&gt;empty&lt;\/body-content&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;attribute&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;name&gt;format&lt;\/name&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;required&gt;false&lt;\/required&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;rtexprvalue&gt;true&lt;\/rtexprvalue&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;\/attribute&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;\/tag&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;tag&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;name&gt;For&lt;\/name&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;tag-class&gt;org.epgpathshala.dad.ForTag&lt;\/tag-class&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;body-content&gt;jsp&lt;\/body-content&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;attribute&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;name&gt;count&lt;\/name&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;required&gt;true&lt;\/required&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;rtexprvalue&gt;true&lt;\/rtexprvalue&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;\/attribute&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;\/tag&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;\/taglib&gt;<\/span><\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; font-size: 1em;\">A sample JSP<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The jsp file demonstrating the use of the two tags in a JSP file is as follows: Code for home.jsp<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;%@ taglib uri=\"\/WEB-INF\/tld\/simple.tld\" prefix=\"simple\" %&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;html&gt;&lt;head&gt;&lt;title&gt;Home page&lt;\/title&gt;&lt;\/head&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;body&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;h1&gt;Home Page&lt;\/h1&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;simple:For count='5'&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;h2&gt;&lt;simple:Now format=\"dd\/MMM\/yyyy\" \/&gt;&lt;\/h2&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">\/simple:For&gt;<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;\/body&gt;<\/span><\/p>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Deploying and Using Custom Tags<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/YZCuWHxUSb4\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"font-size: 1em;\">Suggested Reading:<\/strong><\/p>\r\n\r\n<ol>\r\n \t<li>Core Servlets and Java Server Pages Volume 2 by Marty Hall &amp; Larry Brown, Second Edition, Pearson Education.<\/li>\r\n \t<li style=\"text-align: justify;\">Core JSP by Damon Haugland and Aaron Tavistock, Pearson Education<\/li>\r\n \t<li style=\"text-align: justify;\">Java Server Programming for Professionals by Ivan Bayross, Sh aranam Shah, Cynhthia Bayross and Vaishali Shah Shroff Publishers and Distributros.<\/li>\r\n \t<li style=\"text-align: justify;\">http:\/\/docs.oracle.com\/javaee\/5\/tutorial\/doc\/bnagx.html<\/li>\r\n<\/ol>","rendered":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/YZCuWHxUSb4\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Introduction<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">In the last module, we have seen what is a custom tag, how do we develop a custom tag, we have seen that there were three kinds of custom tags, we have a simple tag, we had a iteration tag and we also have a body tag.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">In this module we just want to see, how do we develop this kind of tags and what is required for deploying them in a web application. Towards the end in the last module we had also seen that we should be creating some kind of a TLD file. We will see, how do we create a TLD file what kind of content goes in a TLD file, and we will also develop a couple of tags.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">We will be developing the tag for getting the current date and time in a JSP. So, in any JSP if I want current date and time, I should be able to show the current date and time of the server, and then we might require formatting, so, we will see how we can do that. Another tag which will be used as a iteration tag., we can have some kind of a count for a ForTag. So, we will be creating two tags.<\/p>\n<p>&nbsp;<\/p>\n<p>NowTag and<\/p>\n<p>ForTag<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The idea is to demonstrate, how do we develop custom tag, and use it in a web application, what is required for deploying it in a web application<\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\"><strong>Demo Of creating Custom Tags\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">In this demo we will develop two custom tags, one is a simple tag, which can be used for showing the current date and time of the server and another tag is a iteration tag, which can be used to loop a number of times specified by a count attribute. We will than create a tld file so that these tags can be used in a JSP page, and then we also develop a small JSP file to show how the tags can be included in a JSP and used.<\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\"><strong>Developing the NowTag class\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The NowTag class is an implementation of the Tag interface. It has an attribute for the formatting the date and time. This is developed as an empty tag, and the doEndTag method is used for generating the otuput according the format attribute.<\/p>\n<p>&nbsp;<\/p>\n<p>The code for NowTag class is as follows:<\/p>\n<p>Code for NowTag class<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">package org.epgpathshala.dad.tag;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">import javax.servlet.*;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">import javax.servlet.jsp.*;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">import javax.servlet.jsp.tagext.*;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">import java.io.IOException;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">import java.util.Date;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">import java.text.SimpleDateFormat;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">public class NowTag implements Tag { private PageContext pageContext; private Tag parent;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">private String format = &#8220;dd\/MM\/yyyy&#8221;;<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>public void setPageContext(PageContext pc) { this.pageContext = pc;<\/p>\n<p>}<\/p>\n<p>public void setParent(Tag p) { this.parent = p;<\/p>\n<p>}<\/p>\n<p>public Tag getParent() {<\/p>\n<p>return this.parent;<\/p>\n<p>}<\/p>\n<p>public void setFormat(String fmt) { this.format = fmt;<\/p>\n<p>}<\/p>\n<p>public int doStartTag() {<\/p>\n<p>return Tag.SKIP_BODY;<\/p>\n<p>}<\/p>\n<p>public int doEndTag() { Date d = new Date();<\/p>\n<p>SimpleDateFormat sdf = new SimpleDateFormat(format); try {<\/p>\n<p>pageContext.getOut().println(sdf.format(d));<\/p>\n<p>} catch(IOException ioe) {<\/p>\n<p>ioe.printStackTrace();<\/p>\n<p>}<\/p>\n<p>return Tag.EVAL_PAGE;<\/p>\n<p>}<\/p>\n<p>public void release() {<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/div>\n<p><strong style=\"text-align: initial; font-size: 1em;\">Developing the For Tag class\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">The ForTag class is an implementation of the IterationTag interface. It has an attribute for the number of times the loop should be done. This is developed to have a body, and the doAfterBody method decrements the count and checks for zero.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">The code for ForTag class is as follows: Code for ForTag class<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">package org.epgpathshala.dad.tag;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">import javax.servlet.*;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">import javax.servlet.jsp.*;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">import javax.servlet.jsp.tagext.*;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">import java.io.IOException;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public class ForTag implements IterationTag { private PageContext pageContext;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">private Tag parent;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">private int count;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void setPageContext(PageContext ctx) { this.pageContext = ctx;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void setParent(Tag p) {<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">this.parent = p;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public Tag getParent() { return this.parent;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void setCount(int c) {<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">this.count = c;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public int doStartTag() {<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">return count&lt;=0?Tag.SKIP_BODY:Tag.EVAL_BODY_INCLUDE;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public int doAfterBody() {<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">count&#8211;;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">return count&lt;=0?Tag.SKIP_BODY:IterationTag.EVAL_BODY_AGAIN;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public int doEndTag() {<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">return Tag.EVAL_PAGE;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void release() {<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\"><span style=\"text-align: initial; font-size: 1em;\">TLD file\u00a0<\/span><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">The tld file for using the two tags is as follows: Code for simple.tld<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;taglib&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;tlib-version&gt;1.0&lt;\/tlib-version&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;jsp-version&gt;1.2&lt;\/jsp-version&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;short-name&gt;Simple&lt;\/short-name&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;tag&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;name&gt;Now&lt;\/name&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;tag-class&gt;org.epgpathshala.dad.NowTag&lt;\/tag-class&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;body-content&gt;empty&lt;\/body-content&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;attribute&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;name&gt;format&lt;\/name&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;required&gt;false&lt;\/required&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;rtexprvalue&gt;true&lt;\/rtexprvalue&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;\/attribute&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;\/tag&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;tag&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;name&gt;For&lt;\/name&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;tag-class&gt;org.epgpathshala.dad.ForTag&lt;\/tag-class&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;body-content&gt;jsp&lt;\/body-content&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;attribute&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;name&gt;count&lt;\/name&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;required&gt;true&lt;\/required&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;rtexprvalue&gt;true&lt;\/rtexprvalue&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;\/attribute&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;\/tag&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;\/taglib&gt;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; font-size: 1em;\">A sample JSP<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The jsp file demonstrating the use of the two tags in a JSP file is as follows: Code for home.jsp<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;%@ taglib uri=&#8221;\/WEB-INF\/tld\/simple.tld&#8221; prefix=&#8221;simple&#8221; %&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;html&gt;&lt;head&gt;&lt;title&gt;Home page&lt;\/title&gt;&lt;\/head&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;body&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;h1&gt;Home Page&lt;\/h1&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;simple:For count=&#8217;5&#8242;&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;h2&gt;&lt;simple:Now format=&#8221;dd\/MMM\/yyyy&#8221; \/&gt;&lt;\/h2&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">\/simple:For&gt;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">&lt;\/body&gt;<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Deploying and Using Custom Tags<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/YZCuWHxUSb4\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"font-size: 1em;\">Suggested Reading:<\/strong><\/p>\n<ol>\n<li>Core Servlets and Java Server Pages Volume 2 by Marty Hall &amp; Larry Brown, Second Edition, Pearson Education.<\/li>\n<li style=\"text-align: justify;\">Core JSP by Damon Haugland and Aaron Tavistock, Pearson Education<\/li>\n<li style=\"text-align: justify;\">Java Server Programming for Professionals by Ivan Bayross, Sh aranam Shah, Cynhthia Bayross and Vaishali Shah Shroff Publishers and Distributros.<\/li>\n<li style=\"text-align: justify;\">http:\/\/docs.oracle.com\/javaee\/5\/tutorial\/doc\/bnagx.html<\/li>\n<\/ol>\n","protected":false},"author":4,"menu_order":31,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["mr-pravin-jain"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-168","chapter","type-chapter","status-publish","hentry","contributor-mr-pravin-jain"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":9,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/168\/revisions"}],"predecessor-version":[{"id":423,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/168\/revisions\/423"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/168\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/media?parent=168"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapter-type?post=168"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/contributor?post=168"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/license?post=168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}