Creating Custom Tags in JSP #Root #By Sayed Ahmed

By Sayed, January 13th, 2008 [use large font in IE/Firefox]

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 tag
2. For the Java file, you have to extend javax.servlet.jsp.tagext.BodyTagSupport class
3. in your implementation, you have to override (re-write) doStartTag(), doEndTag(), 
and doAfterBody() methods
4. Create tag library description file(XML file with .tld extension).

Example
-------

Implement a tag that reverses a string


import 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

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.
//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
    <tlibversion>1.0</tlibversion>
    <jspversion>1.1</jspversion>
    <shortname></shortname>
    <info></info>
    <tag>
        <name>stringreverse</name>
        <tagclass>net.justetc.taglibrary.ReverseTag</tagclass>
        <info>
            Reverse the text
        </info>
    </tag>
</taglib>


6. Example use of the custom tag


<%@ taglib uri="/WEB-INF/reverse.tld" prefix="reverse" %>

<html>
<head>
    <title>Custom Tag library</title>
</head>

<body bgcolor="#ffffff">

<hr />
<reverse:stringreverse>
        justetc
</reverse:stringreverse>
<hr />
</body>
</html>


From: http://sitestree.com/?p=3645
Categories:Root, By Sayed Ahmed
Tags:
Post Data:2016-09-15 14:21:09

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada