Creating Custom Tags in JSP #37

How to create custom tags in JSP?You can create custom tags in JSP. Steps:-----1. You have to create a Java file that will define the operation of the custom tag2. For the Java file, you have to extend javax.servlet.jsp.tagext.BodyTagSupport class3. in your implementation, you have to override (re-write) doStartTag(), doEndTag(), and doAfterBody() methods4. Create tag library description file(XML file with .tld extension).Example-------Implement a tag that reverses a stringimport java.io.IOException;import javax.servlet.jsp.JspTagException;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.tagext.BodyContent;import javax.servlet.jsp.tagext.BodyTagSupport;public class ReverseTag extends BodyTagSupport {    private static final long serialVersionUID = 1L;            //override doStartTag    public int doStartTag() throws JspTagException{        return EVAL_BODY_TAG;    }            //override doEndTag    public int doEndTag() throws JspTagException     {      try {           JspWriter out = pageContext.getOut();      } catch (Exception ex) {           throw new JspTagException("Exception" + ex);      }    return SKIP_BODY; }//reverse the text    public int doAfterBody() throws JspTagException {    BodyContent body = getBodyContent();    try {            JspWriter out = body.getEnclosingWriter();            //get text inside the tag            String bodyContent = body.getString();            //reverse the text            if (bodyContent != null) {                for (int i = bodyContent.length() - 1; i >= 0; i--) {                    out.print(bodyContent.charAt(i));                }            }            out.println();            body.clearBody(); // Clear for next evaluation          } catch (IOException ioe) {               throw new JspTagException("Exception at doAfterBody " + ioe);     }        return (SKIP_BODY);    }}5. Create the taglib descriptor    1.0    1.1                    stringreverse        net.justetc.taglibrary.ReverseTag                    Reverse the text            6. Example use of the custom tag    Custom Tag library
justetc

From: http://sitestree.com/?p=4791
Categories:37
Tags:
Post Data:2010-05-23 22:42:48

    Shop Online: <a href='https://www.ShopForSoul.com/' target='new' rel="noopener">https://www.ShopForSoul.com/</a>
    (Big Data, Cloud, Security, Machine Learning): Courses: <a href='http://Training.SitesTree.com' target='new' rel="noopener"> http://Training.SitesTree.com</a> 
    In Bengali: <a href='http://Bangla.SaLearningSchool.com' target='new' rel="noopener">http://Bangla.SaLearningSchool.com</a>
    <a href='http://SitesTree.com' target='new' rel="noopener">http://SitesTree.com</a>
    8112223 Canada Inc./JustEtc: <a href='http://JustEtc.net' target='new' rel="noopener">http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning) </a>
    Shop Online: <a href='https://www.ShopForSoul.com'> https://www.ShopForSoul.com/</a>
    Medium: <a href='https://medium.com/@SayedAhmedCanada' target='new' rel="noopener"> https://medium.com/@SayedAhmedCanada </a>