Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

Attributes


There can be defined too many attributes for any custom tag. To define the attribute, you need to perform two tasks:

  • Define the property in the TagHandler class with the attribute name and define the setter method
  • define the attribute element inside the tag element in the TLD file

Let’s understand the attribute by the tag given below:

  1. <m:cube number=“4”></m:cube>

Here m is the prefix, cube is the tag name and number is the attribute.


Simple example of attribute in JSP Custom Tag

In this example, we are going to use the cube tag which return the cube of any given number. Here, we are defining the number attribute for the cube tag. We are using the three file here:

  • index.jsp
  • CubeNumber.java
  • mytags.tld

index.jsp

  1. <%@ taglib uri=“WEB-INF/mytags.tld” prefix=“m” %>
  2. Cube of 4 is: <m:cube number=“4”></m:cube>

CubeNumber.java

  1. package com.Kreationnext.taghandler;
  2. import javax.servlet.jsp.JspException;
  3. import javax.servlet.jsp.JspWriter;
  4. import javax.servlet.jsp.tagext.TagSupport;
  5. public class CubeNumber extends TagSupport{
  6. private int number;
  7. public void setNumber(int number) {
  8.     this.number = number;
  9. }
  10. public int doStartTag() throws JspException {
  11.     JspWriter out=pageContext.getOut();
  12.     try{
  13.         out.print(number*number*number);
  14.     }catch(Exception e){e.printStackTrace();}
  15.     return SKIP_BODY;
  16. }
  17. }

mytags.tld

  1. <?xml version=“1.0” encoding=“ISO-8859-1” ?>
  2. <!DOCTYPE taglib
  3.         PUBLIC “-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN”
  4.         “http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd”>
  5. <taglib>
  6.   <tlib-version>1.0</tlib-version>
  7.   <jsp-version>1.2</jsp-version>
  8.   <short-name>simple</short-name>
  9.   <uri>http://tomcat.apache.org/example-taglib</uri>
  10.   <description>A simple tab library for the examples</description>
  11.   <tag>
  12.     <name>cube</name>
  13.     <tag-class>com.Kreationnext.taghandler.CubeNumber</tag-class>
  14.     <attribute>
  15.     <name>number</name>
  16.     <required>true</required>
  17.     </attribute>
  18.   </tag>
  19. </taglib>

Output

  1. Cube of 4 is: 64
download this example

JSP Custom Tag attribute example with database

Let’s create a custom tag that prints a particular record of table for the given table name and id.

So, you have to have two properties in the tag handler class.

PrintRecord.java

  1. package com.Kreationnext;
  2. import javax.servlet.jsp.JspException;
  3. import javax.servlet.jsp.JspWriter;
  4. import javax.servlet.jsp.tagext.TagSupport;
  5. import java.sql.*;
  6. public class PrintRecord extends TagSupport{
  7. private String id;
  8. private String table;
  9. public void setId(String id) {
  10.     this.id = id;
  11. }
  12. public void setTable(String table) {
  13.     this.table = table;
  14. }
  15. public int doStartTag()throws JspException{
  16.     JspWriter out=pageContext.getOut();
  17.     try{
  18.         Class.forName(“oracle.jdbc.driver.OracleDriver”);
  19.         Connection con=DriverManager.getConnection(
  20.                  “jdbc:oracle:thin:@localhost:1521:xe”,“system”,“oracle”);
  21.         PreparedStatement ps=con.prepareStatement(“select * from “+table+” where id=?”);
  22.         ps.setInt(1,Integer.parseInt(id));
  23.         ResultSet rs=ps.executeQuery();
  24.         if(rs!=null){
  25.         ResultSetMetaData rsmd=rs.getMetaData();
  26.         int totalcols=rsmd.getColumnCount();
  27.         //column name
  28.         out.write(“<table border=’1′>”);
  29.         out.write(“<tr>”);
  30.         for(int i=1;i<=totalcols;i++){
  31.             out.write(“<th>”+rsmd.getColumnName(i)+“</th>”);
  32.         }
  33.         out.write(“</tr>”);
  34.         //column value
  35.         if(rs.next()){
  36.             out.write(“<tr>”);
  37.                 for(int i=1;i<=totalcols;i++){
  38.                 out.write(“<td>”+rs.getString(i)+“</td>”);
  39.             }
  40.             out.write(“</tr>”);
  41.         }else{
  42.             out.write(“Table or Id doesn’t exist”);
  43.         }
  44.         out.write(“</table>”);
  45.         }
  46.         con.close();
  47.     }catch(Exception e){System.out.println(e);}
  48.     return SKIP_BODY;
  49. }
  50. }

m.tld

  1. <?xml version=“1.0” encoding=“ISO-8859-1” ?>
  2. <!DOCTYPE taglib
  3.         PUBLIC “-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN”
  4.     “http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd”>
  5. <taglib>
  6.   <tlib-version>1.2</tlib-version>
  7.   <jsp-version>2.0</jsp-version>
  8.   <short-name>c</short-name>
  9.   <uri>Kreationnext</uri>
  10. <tag>
  11. <name>printRecord</name>
  12. <tag-class>com.Kreationnext.PrintRecord</tag-class>
  13. <attribute>
  14. <name>id</name>
  15. <required>true</required>
  16. </attribute>
  17. <attribute>
  18. <name>table</name>
  19. <required>true</required>
  20. </attribute>
  21. </tag>
  22. </taglib>

index.jsp

  1. <%@ taglib uri=“Kreationnext” prefix=“j” %>
  2. <j:printRecord table=“user874” id=“1”></j:printRecord>

Output

customtagattribute

Comments

comments