Open In App

Custom Tags with Attributes in JSP

Last Updated : 30 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Custom tags are user-defined action tags that can be used within Java Server Pages. A custom tag may be defined with a number of attributes for providing additional information to perform the operation associated with the tag. Each attribute is mapped to a property in the tag handler. While implementing the tag handler, getters and setters for these properties must be provided.

After creating the object of the tag handler using the no-arg constructor, the JSP container calls the setter methods for all the properties of the tag handler and sets the value obtained from the corresponding attribute. Once the properties are set, operations are performed on the tag handler object.

For classic tag handlers(tag handlers that implement/extend the Tag interface or its sub-interfaces/sub-classes), a single tag handler object is used for multiple occurrences of the tag on the page. If the values of attributes differ for two different occurrences, the properties of the tag handler are reset. These attributes should be declared in the Tag Library Descriptor as well. <attribute> tag is used to encapsulate all the properties of the attribute. <attribute> consists of the following sub-elements :

  • <name>: used to provide a unique name to the attribute.
  • <required>: used to specify whether the attribute is required or optional. Takes a boolean value.
  • <rtexprvalue>: used to specify whether runtime expression can be fed into the value of the attribute. Takes a boolean value.
  • <type>: used to specify the type of value that is to be provided to the attribute. Default is taken as String.
  • <fragment>: used to specify whether the attribute stores a JspFragment or not.

Example Project

SumTagHandler.java

Java




package tagHandler;
  
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
  
public class SumTagHandler extends SimpleTagSupport {
    
    double num1;
    double num2;
      
    public double getNum1() {
        return num1;
    }
  
    public void setNum1(double num1) {
        this.num1 = num1;
    }
  
    public double getNum2() {
        return num2;
    }
  
    public void setNum2(double num2) {
        this.num2 = num2;
    }
  
    public void doTag() throws JspException,IOException
    {
        JspWriter out=getJspContext().getOut();
        double sum=num1+num2;
        out.println(String.format("%.2f", sum));
    }
}


DiffTagHandler.java

Java




package tagHandler;
  
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
  
public class DiffTagHandler extends SimpleTagSupport {
    
    double num1;
    double num2;
      
    public double getNum1() {
        return num1;
    }
  
    public void setNum1(double num1) {
        this.num1 = num1;
    }
  
    public double getNum2() {
        return num2;
    }
  
    public void setNum2(double num2) {
        this.num2 = num2;
    }
  
    public void doTag() throws JspException,IOException
    {
        JspWriter out=getJspContext().getOut();
        double diff=num1-num2;
        out.println(String.format("%.2f",diff));
    }
}


ProductTagHandler.java

Java




package tagHandler;
  
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
  
public class ProductTagHandler extends SimpleTagSupport {
    
    double num1;
    double num2;
      
    public double getNum1() {
        return num1;
    }
  
    public void setNum1(double num1) {
        this.num1 = num1;
    }
  
    public double getNum2() {
        return num2;
    }
  
    public void setNum2(double num2) {
        this.num2 = num2;
    }
  
    public void doTag() throws JspException,IOException
    {
        JspWriter out=getJspContext().getOut();
        double product=num1*num2;
        out.println(String.format("%.2f", product));
    }
}


QuotientTagHandler.java

Java




package tagHandler;
  
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
  
public class QuotientTagHandler extends SimpleTagSupport {
    
    double num1;
    double num2;
      
    public double getNum1() {
        return num1;
    }
  
    public void setNum1(double num1) {
        this.num1 = num1;
    }
  
    public double getNum2() {
        return num2;
    }
  
    public void setNum2(double num2) {
        this.num2 = num2;
    }
  
    public void doTag() throws JspException,IOException
    {
        JspWriter out=getJspContext().getOut();
        double quotient=num1/num2;
        out.println(String.format("%.2f",quotient));
    }
}


ModuloTagHandler.java

Java




package tagHandler;
  
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
  
public class ModuloTagHandler extends SimpleTagSupport {
    
    int num1;
    int num2;
      
    public int getNum1() {
        return num1;
    }
  
    public void setNum1(int num1) {
        this.num1 = num1;
    }
  
    public int getNum2() {
        return num2;
    }
  
    public void setNum2(int num2) {
        this.num2 = num2;
    }
  
    public void doTag() throws JspException,IOException
    {
        JspWriter out=getJspContext().getOut();
        int modulo=num1%num2;
        out.println(modulo);
    }
}


PowTagHandler.java

Java




package tagHandler;
  
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
  
public class PowTagHandler extends SimpleTagSupport {
    
    double num1;
    double num2;
      
    public double getNum1() {
        return num1;
    }
  
    public void setNum1(double num1) {
        this.num1 = num1;
    }
  
    public double getNum2() {
        return num2;
    }
  
    public void setNum2(double num2) {
        this.num2 = num2;
    }
  
    public void doTag() throws JspException,IOException
    {
        JspWriter out=getJspContext().getOut();
        double pow=Math.pow(num1,num2);
        out.println(String.format("%.2f", pow));
    }
}


mathTagLib.tld

XML




<taglib version="2.0" xmlns="http://java.sun.com/xml/j2ee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
  <tlib-version>2.0</tlib-version>
  <jsp-version>2.0</jsp-version>
  <tag>
      <name>sum</name>
      <tagclass>tagHandler.SumTagHandler</tagclass>
      <body-content>empty</body-content>
      <attribute>
          <name>num1</name>
          <type>double</type>
          <rtexprvalue>true</rtexprvalue>
          <required>true</required>
      </attribute>
      <attribute>
          <name>num2</name>
          <type>double</type>
          <rtexprvalue>true</rtexprvalue>
          <required>true</required>
      </attribute>
  </tag>
  <tag>
      <name>diff</name>
      <tagclass>tagHandler.DiffTagHandler</tagclass>
      <body-content>empty</body-content>
      <attribute>
          <name>num1</name>
          <type>double</type>
          <rtexprvalue>true</rtexprvalue>
          <required>true</required>
      </attribute>
      <attribute>
          <name>num2</name>
          <type>double</type>
          <rtexprvalue>true</rtexprvalue>
          <required>true</required>
      </attribute>
  </tag>
  <tag>
      <name>product</name>
      <tagclass>tagHandler.ProductTagHandler</tagclass>
      <body-content>empty</body-content>
      <attribute>
          <name>num1</name>
          <type>double</type>
          <rtexprvalue>true</rtexprvalue>
          <required>true</required>
      </attribute>
      <attribute>
          <name>num2</name>
          <type>double</type>
          <rtexprvalue>true</rtexprvalue>
          <required>true</required>
      </attribute>
  </tag>
  <tag>
      <name>quotient</name>
      <tagclass>tagHandler.QuotientTagHandler</tagclass>
      <body-content>empty</body-content>
      <attribute>
          <name>num1</name>
          <type>double</type>
          <rtexprvalue>true</rtexprvalue>
          <required>true</required>
      </attribute>
      <attribute>
          <name>num2</name>
          <type>double</type>
          <rtexprvalue>true</rtexprvalue>
          <required>true</required>
      </attribute>
  </tag>
  <tag>
      <name>modulo</name>
      <tagclass>tagHandler.ModuloTagHandler</tagclass>
      <body-content>empty</body-content>
      <attribute>
          <name>num1</name>
          <type>int</type>
          <rtexprvalue>true</rtexprvalue>
          <required>true</required>
      </attribute>
      <attribute>
          <name>num2</name>
          <type>int</type>
          <rtexprvalue>true</rtexprvalue>
          <required>true</required>
      </attribute>
  </tag>
  <tag>
      <name>pow</name>
      <tagclass>tagHandler.PowTagHandler</tagclass>
      <body-content>empty</body-content>
      <attribute>
          <name>num1</name>
          <type>double</type>
          <rtexprvalue>true</rtexprvalue>
          <required>true</required>
      </attribute>
      <attribute>
          <name>num2</name>
          <type>double</type>
          <rtexprvalue>true</rtexprvalue>
          <required>true</required>
      </attribute>
  </tag>
</taglib>


math.jsp

HTML




<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
<%@taglib uri="WEB-INF/mathTagLib.tld" prefix="math" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>Arithmetic Operations</title>
   </head>
   <body>
      <h2>Arithmetic Operations using Custom Tags</h2>
      2.7 + 3.1 =
      <math:sum num1="2.7" num2="3.1"/>
      <br><br>
      3.8 - 1.6 = 
      <math:diff num1="3.8" num2="1.6"/>
      <br><br>
      2.1 * 5.3 = 
      <math:product num1="2.1" num2="5.3"/>
      <br><br>
      10.8 / 3 = 
      <math:quotient num1="10.8" num2="3"/>
      <br><br>
      5 % 2 = 
      <math:modulo num1="5" num2="2"/>
      <br><br>
      2.64 raised to power 5 = 
      <math:pow num1="2.64" num2="5"/>
   </body>
</html>


Output:

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads