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.
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.

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") requiredetcAlso we will have a label for our input tags so then
then according to these attributes we have to write public variables in our Custom Tag class for eg
(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
then we can write the desired code we want to display in our
out.println();for eg:
//////this would be the line which would be accessed using the custom tag
then surround it with try catch
And in your
web-xmlfile create a tag descriptor library fileeg(myLib.tld)and then copy the following code I have given belowthen write the tag details; first write start tag
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
And then like this you write the remaining and close the above
</taglib>'s tagthe 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)then you just have to import the tag library descriptor file in your jsp
like this
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
(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
}
myLib.tld is mentioned above
jsp page code(form)