package net.viralpatel.jsp.custom.taglib;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class SubstrTagHandler extends TagSupport {
private String input;
private int start;
private int end;
@Override
public int doStartTag() throws JspException {
try {
//Get the writer object for output.
JspWriter out = pageContext.getOut();
//Perform substr operation on string.
out.println(input.substring(start, end));
} catch (IOException e) {
e.printStackTrace();
}
return SKIP_BODY;
}
public String getInput() {
return input;
}
public void setInput(String input) {
this.input = input;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
}
Code language: Java (java)
In above code we have created three attributes: input, start and end. These are the inputs that we will get when the custom tag will be invoked from a JSP file. Also note that, these attributes have getter and setter methods which will be used to set the property values. Now we will create the tag descriptor file also called TLD. Create a file called SubstrDescriptor.tld in WEB-INF directory of your web project and copy / paste following content in it. <?xml version="1.0" encoding="UTF-8"?>
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>substr</shortname>
<info>Sample taglib for Substr operation</info>
<uri>https://www.viralpatel.net/jsp/taglib/substr</uri>
<tag>
<name>substring</name>
<tagclass>net.viralpatel.jsp.custom.taglib.SubstrTagHandler</tagclass>
<info>Substring function.</info>
<attribute>
<name>input</name>
<required>true</required>
</attribute>
<attribute>
<name>start</name>
<required>true</required>
</attribute>
<attribute>
<name>end</name>
<required>true</required>
</attribute>
</tag>
</taglib>
Code language: HTML, XML (xml)
In above tld file, <tag> is used to define a custom tag. Each new tag will have its own tag handler class which we specify in <tagclass> tag. Also <name> tag in <tag> represents the tag name that we use in jsp file. We have provided the three attributes with this tag: input, start and end. input is the String whose sub string needs to be parsed. start is the start index and end is the end index. We have just created our first Custom Taglib for JSP. Now let us use the custom taglib in a JSP file. For this create index.jsp in your web application (if it exists, modify it and add this code) and add following code in it. <required> true make these attribute mandatory. <%@taglib prefix="test" uri="/WEB-INF/SubstrDescriptor.tld"%>
<html>
<head>
<title>JSP Custom Taglib example: Substr function</title>
</head>
<body>
SUBSTR(GOODMORNING, 1, 6) is
<font color="blue">
<test:substring input="GOODMORNING" start="1" end="6"/>
</font>
</body>
</html>
Code language: HTML, XML (xml)
Following is the screenshot of the output. Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
I tried your example but then it fails since it does not recognise the url part in the tld file.
include that .tld in ur web.xml
You should include this library http://tomcat.apache.org/download-taglibs.cgi , in your [WEB-INF/lib] directory then add the jar in the build path of your project.....afterward it should work!!
thank you for the explanation I wish if you put more examples, some real examples i mean the ones you usually use in your code, because i cannot understand exactly why i should use it when I can use EL function
Its gud to provide such facility.thank u.
thanks.
I tried your example and it works perfectly fine for me. Thanks for posting this.
This is really a very good site. The example given is very fine and it worked very well.
Thank you very much :)
This is very clear and helpful. Thanks for posting :)
it is very clear:)
Very Good and quite simple implementation. Thanks Dude
Thanks...:-)
its easy to understand..
Thanks Viral. The tutorial was quite helpful.