Why my custom tag is appearing multiple times in jsp?

129 views Asked by At

I am using a custom tag in my code to take user input but the jsp page has multiple inputs. What is the reason for it?

Also please suggest me a proper documentation or any resources to learn about these tags in detail.

This is the output image.

enter image description here

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="/WEB-INF/myLib.tld" prefix="input" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
        <h1>JSP page data</h1>
        <hr>
        <form action="success.jsp" method="post">
            <input:iemail email=""></input:iemail>
            <input:iid id=""></input:iid>
            <input:iname name=""></input:iname>
            <input:ipass password=""></input:ipass>
            <input type="submit" value="Submit"/>
        </form>
        
</body>
</html>

myLib.tld

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>

    <tlib-version>1.0</tlib-version>
    <jsp-version>2.0</jsp-version>
    <short-name>testing</short-name>
    <uri>http://www.tomcat-demo.com/testing</uri>
    <description>This is a demonstration tag library</description>

    <tag>
        <name>iname</name>
        <tag-class>CustTag.CustTag</tag-class>
        
        <attribute>
        <name>name</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    
    <tag>
        <name>iid</name>
        <tag-class>CustTag.CustTag</tag-class>
        
        <attribute>
        <name>id</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    
    <tag>
        <name>iemail</name>
        <tag-class>CustTag.CustTag</tag-class>
            
        <attribute>
        <name>email</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    <tag>
        <name>ipass</name>
        <tag-class>CustTag.CustTag</tag-class>
        
        <attribute>
        <name>password</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    
</taglib>
package CustTag;

import java.io.IOException;

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

public class CustTag extends TagSupport{
    private int id;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    private String name;
    private String email;
    private String password;
    @Override
    public int doStartTag() throws JspException {
        JspWriter out = pageContext.getOut();
        
        try {
            out.println("<h1>JAVA File Code</h1>");
            out.println("<input type='number' name='"+id+"'>");
            out.println("<input type='text' name='"+name+"'>");
            out.println("<input type='email' name='"+email+"'>");
            out.println("<input type='password' name='"+password+"'>");
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return SKIP_BODY;
        
    }

}

The texfields and numberfields should appear only once not repeated.

1

There are 1 answers

0
ni9khil On

How to create a Custom Tags in JSP. I am creating a custom input tag in JSP.

So first we will have to import the TagSupport jar files and then We have to create a Java Class for our tags. (eg. MyCustomTag.java)

Then we have to inherit the TagSupport class in that library to our CustomTag java class.

Then we will override the doStartTag method of the SuperClass TagSupport class.

This is important step we will create a variables which we want in our html. Let's say if we want to create an input field which have different attributes like <input attr1(type="text") attr2(id="elmntid") attr3(name="elemntname") required etc

Also we will have a label for our input tags so then

<label > Attribut(labelname of inputfield)-->lets take "title">

then according to these attributes we have to write public variables in our Custom Tag class for eg

public String type;
public String id;
public String style;
public String name;
public String title;

(took title for label bcoz Label would be confusing)

then In your IDE click on generate getter and setter of these variables.

After that there's a class in Java

JspWriter out = pageContext.getOut();

then we can write the desired code we want to display in our out.println();

for eg:

out.print("<label>"+title+"</label>");
out.println("<input type='"+ type +"' name = "'+ elmntname +'" id='"+ elmntid +"'>");

//////this would be the line which would be accessed using the custom tag

then surround it with try catch

And in your web-xml file create a tag descriptor library file eg(myLib.tld) and then copy the following code I have given below

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library               1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>testing</short-name>
<uri>http://www.tomcat-demo.com/testing</uri>
<description>This is a demonstration tag library</description>

then write the tag details; first write start tag

<tag>
<name>mytag</name>           ----> this is your tag name
<tag-class>(package_name.classname)CustomTagPackage.MyCustomTag.java</tag-   class>

and then start writing attribute of these class which you had taken as variables in your Java file;

and write your attribute name and then write it is required or not means the front end developer which is going to use this tag he should use the tag you have mentioned as required.

So it would be

<attribute>
        <name>name</name>
        <required>true</required>
    </attribute>

    <attribute>
        <name>id</name>
        <required>true</required>
    </attribute>

And then like this you write the remaining and close the above </taglib> 's tag

the code would be like this of

myLib.tld (some of the commented tags are not created in java file so you can create one if you want them)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library  1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>testing</short-name>
<uri>http://www.tomcat-demo.com/testing</uri>
<description>This is a demonstration tag library</description>
<tag>
    <name>mytag</name>
    <tag-class>MyCustomTag.MyCustomTag</tag-class>

    <attribute>
        <name>name</name>
        <required>true</required>
    </attribute>

    <attribute>
        <name>id</name>
        <required>true</required>
    </attribute>

    <attribute>
        <name>title</name>
        <required>false</required>
    </attribute>

    <attribute>
        <name>type</name>
        <required>true</required>
    </attribute>

    //<attribute>
        <name>placeholder</name>
        <required>false</required>
    //</attribute>

    <attribute>
        <name>style</name>
        <required>false</required>
    </attribute>

    //<attribute>
        <name>requiretag</name>
    //</attribute>
</tag>
</taglib>

then you just have to import the tag library descriptor file in your jsp

like this

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

and you have to use the prefix name (mtprefix by which you can access your custom tag)

save your jsp and then you're ready to use your custom tag in your java file

in jsp wherever you just enter "<" and hit ctrl+spce it will show you your custom tag. And then if you enter it, it would come up as you have designed in your java code.

for eg

<mtprefix:mytag title="Enter email:" name="stdemail" type="email" id="stdemail"    placeholder="[email protected]" style=""></mtprefix:mytag>

(Note: title is written as a Label name) Here, in above tag code you can pass whichever values just like your normal html code. And you can use your tag as much time as you want.

You can also use StringBuffer for memory efficiency and then adding it to out.print();

Full Code Here

MyCustomTag.java

package MyCustomTag;

import java.io.IOException;

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

public class MyCustomTag extends TagSupport {
public String type;
public String id;
public String name;
public String title;
public String placeholder;
public String style;
public boolean requiretag;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}


public boolean isRequiretag() {
    return requiretag;
}

public void setRequiretag(boolean requiretag) {
    this.requiretag = requiretag;
}

public String getStyle() {
    return style;
}

public void setStyle(String style) {
    this.style = style;
}

public String getPlaceholder() {
    return placeholder;
}

public void setPlaceholder(String placeholder) {
    this.placeholder = placeholder;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public String gettitle() {
    return title;
}

public void settitle(String title) {
    this.title = title;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Override
public int doStartTag() throws JspException {
    JspWriter out = pageContext.getOut();

    StringBuffer sb = new StringBuffer();
    try {
            sb.append("<label style='width:200px;' 'padding-left:70px;'>" +   title + "</label>");
            sb.append("<input type='" + type + "' name='" + name + "' id= '" + id + "' placeholder= '" + placeholder + "' ><br>");
        }
        out.println(sb);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return SKIP_BODY;
}

}

myLib.tld is mentioned above

jsp page code(form)

    <form class="" action="login.do" id="myform">
                <div class="bodyofmodal" style="text-align: left;">
                    <mtprefix:mytag title="Enter id:" name="stdid" id="stdid"
                        type="number" placeholder="Enter three digit ID"
                         style=""></mtprefix:mytag>



                    <mtprefix:mytag title="Enter Name:" name="stdname"   type="text" id="stdname" placeholder="John Doe" style=""></mtprefix:mytag>



                    <mtprefix:mytag title="Enter email:" name="stdemail" type="email"  id="stdemail" placeholder="[email protected]" style=""></mtprefix:mytag>



                    <mtprefix:mytag title="Password:" name="stdpassword"
                        type="password" id="stdpassword" placeholder="Enter Password"
                        style="" ></mtprefix:mytag>
                </div>