There may be described too many attributes for any custom tag. To define the characteristic, you want to carry out tasks:
Let's recognize the characteristic through the tag given below:
<m:cube number="4"></m:cube>
Here, prefix is m, tag name is cube and attribute is the number.
In this example, we're going to use the cube tag which give back the cube of any given quantity. Here, we're defining the quantity characteristic for the cube tag. We are the use of the 3 document here:
index.jsp
<%@ taglib uri="WEB-INF/mytags.tld" prefix="m" %> Cube of 4 is: <m:cube number="4"></m:cube> CubeNumber.java package com.javatpoint.taghandler; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; public class CubeNumber extends TagSupport{ private int number; public void setNumber(int number) { this.number = number; } public int doStartTag() throws JspException { JspWriter out=pageContext.getOut(); try{ out.print(number*number*number); }catch(Exception e){e.printStackTrace();} return SKIP_BODY; } }
mytags.tld
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>simple</short-name> <uri>http://tomcat.apache.org/example-taglib</uri> <description>A simple tab library for the examples</description> <tag> <name>cube</name> <tag-class>com.javatpoint.taghandler.CubeNumber</tag-class> <attribute> <name>number</name> <required>true</required> </attribute> </tag> </taglib> Result Cube of 4 is : 64
Let's create a custom tag that prints a specific document of desk for the given table name and id.
So, you need to have properties withinside the tag handler class.
PrintRecord.java
package com.javatpoint; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; import java.sql.*; public class PrintRecord extends TagSupport{ private String id; private String table; public void setId(String id) { this.id = id; } public void setTable(String table) { this.table = table; } public int doStartTag()throws JspException{ JspWriter out=pageContext.getOut(); try{ Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe","system","oracle"); PreparedStatement ps=con.prepareStatement("select * from "+table+" where id=?"); ps.setInt(1,Integer.parseInt(id)); ResultSet rs=ps.executeQuery(); if(rs!=null){ ResultSetMetaData rsmd=rs.getMetaData(); int totalcols=rsmd.getColumnCount(); //column name out.write("<table border='1'>"); out.write("<tr>"); for(int i=1;i<=totalcols;i++){ out.write("<th>"+rsmd.getColumnName(i)+"</th>"); } out.write("</tr>"); //column value if(rs.next()){ out.write("<tr>"); for(int i=1;i<=totalcols;i++){ out.write("<td>"+rs.getString(i)+"</td>"); } out.write("</tr>"); }else{ out.write("Table or Id doesn't exist"); } out.write("</table>"); } con.close(); }catch(Exception e){System.out.println(e);} return SKIP_BODY; } }
m.tld
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.2</tlib-version> <jsp-version>2.0</jsp-version> <short-name>c</short-name> <uri>javatpoint</uri> <tag> <name>printRecord</name> <tag-class>com.javatpoint.PrintRecord</tag-class> <attribute> <name>id</name> <required>true</required> </attribute> <attribute> <name>table</name> <required>true</required> </attribute> </tag> </taglib>
index.jsp
<%@ taglib uri="javatpoint" prefix="j" %> <j:printRecord table="user874" id="1"></j:printRecord>
Output