Making of Custom TagLib in JSP

    Document   : index
    Created on : Sep 23, 2011, 6:05:28 PM
    Author     : Taher_JAVAHUNTER
<!--Author : Taher_JavaHunter-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <%@taglib prefix="tt" uri=""%>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="jquery-1.4.3.min.js"></script>
        <script type="text/javascript">            
            function txtBoxEmpty(control,length,srno){
                var tbool=true;
                    $(control).parent().append("<div class='err"+srno+"' style='color:red;'>Please Enter Value</div>");
                    tbool = false;
                            $(control).parent().append("<div class='err"+srno+"' style='color:red;'>Max "+length+" Characters Allowed</div>");
                            tbool = false;
                return tbool;
            <table border="5" align="center">
                    <td>Name : </td>
                        <tt:inputText fieldid="test" fieldname="test" length="5" componentno="1"/>
                    <td>Surname : </td>
                        <tt:inputText fieldid="test" fieldname="test" length="10" componentno="2"/>

TabLib Class :

package com.taher.customtag;


import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class InputTextHandler extends TagSupport {
  private String fieldid;
  private String fieldname;
  private String fieldvalue="";
  private String length;
  private String componentno;

    public String getFieldname() {
        return fieldname;

    public void setFieldname(String fieldname) {
        this.fieldname = fieldname;

    public String getFieldvalue() {
        return fieldvalue;

    public void setFieldvalue(String fieldvalue) {
        this.fieldvalue = fieldvalue;

    public String getFieldid() {
        return fieldid;

    public void setFieldid(String fieldid) {
        this.fieldid = fieldid;

    public String getComponentno() {
        return componentno;

    public void setComponentno(String componentno) {
        this.componentno = componentno;

    public String getLength() {
        return length;

    public void setLength(String length) {
        this.length = length;

    public int doStartTag() throws JspException {

        try {
            JspWriter out = pageContext.getOut();
            out.println("<input type='text' id='"+fieldid+"' name='"+fieldname+"' value='"+fieldvalue+"' onblur='txtBoxEmpty(this,"+length+","+componentno+")'/>");
        } catch (IOException e) {
            System.out.println("Error : "+e);
        return SKIP_BODY;

TLD to be added to WEB-INF : InputTextDescriptor.tld

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="" xmlns:xsi="" xsi:schemaLocation="">
        <info>Html Input Text.</info>
This entry was posted in JSP, Just 4 Knowledge. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s