Updated April 7, 2023
Introduction to JSP Tag Library
Tag library defined as a set of collections custom actions and user-defined actions the tags also can be used with developers directly the coder’s code manually code in the JSP web page and automatically predefined tags shown in some Java development tools. A tag library should must with portable libraries and it between different from JSP container implementations. The custom tag libraries or user-defined tag libraries cover the entire basic frameworks that related to the vendors and also it can be used to provide their own tag libraries and concluding with some comparison of standard runtime tags libraries difference between the vendor-specific compile-time tags.
Syntax:
The jsp tag libraries classified into different categories like JSTL(jsp standard tag library) etc. The general syntax is as follows.
<%@ taglib prefix = "" uri = "" %>
<html>
<body>
< ----core tags with their attributes values------/>
----some logic jsp codes----
</body>
</html>
Above basic code is one of the syntaxes for JSTL tag libraries we may use customized tag libraries and pre-defined functions based on the project and user requirements.
How Does JSP Tag Library Work?
The JSP tag library like jstl it represents a set of predefined tags to simplified the jsp codes especially in the development process jstl mainly provides a lot of tags for simplifying the JSP codes is one of the advantages likewise similar to other merits like code-reusability we can use the jstl codes into parallel dynamic web pages we have not use any scriptlet tags in the jstl it is the main advantage of the jstl.
The JSP tag libraries have two different types:
- Standard Tag Library Framework
- Compile Time Tags.
The standard tag library framework is used in the standard java server pages also allows vendors to create dynamic customized jsp tag libraries. It defines as a set of collection custom or user-defined actions. The custom tags also be used directly with the developers or coders. The tags will be defined and the library are mentioned in the tag library description file the URI in the taglib directives specifies and where to find the tag library description file. We also used URI shortcuts in the web.xml configuration file in the jsp projects.
Uses of JSP Tag Library
Below are the different uses of JSP Tag Library:
1. The prefix of the jsp tag Library: libraries in the directives is always used in the strings it’s always used in the JSP web page with any predefined tags from the library the tag library is specified and initialized at the starting of the JSP page document.
2. We can use customized tags like “mytag” is one of the tag in the custom tag libraries we can use jstl tag directives and custom tag libraries prefix it in the JSP translator also used mytag in the tag library description file that can be found at the URI Specified in the taglib directives.
3. The entries of the specified tag in the tag library description file provide specifications about the usages of the tags it includes whether the tag uses attributes and the names of those attributes in the JSTL tags.
4. General the semantics of the tags and the actions that have to have occurred with the results of using the specified tags are defined in the tag handler class it has to be described in the Tag Handlers each specified tag has its own tag handler class and the specified class name is defined in the tag library descriptor file.
4. The file indicates whether a tag uses a body in the tag library descriptor file the custom tag actions can create one or more server-side instances that have been used by the tag itself or by other JSP scripting elements like scriptlets.
6. These objects are generally defined as the scripting variables this variable information is stored in the tag-extra-info class. We can use nested tag handler class outside of the tags this is required in case it’s about the state management of the purpose of the nested tags of Accessing the outer tag handler instances.
7. The JSTL tag handler used for the actions that result from the use of the custom tags and the tag handler is an instance of the java classes that have to be implemented with one of two standard java interfaces it depends on the body of every tag.
Examples to Implement of JSP Tag Library
Below are the examples:
Example #1
Code:
<%@tagliburi="WEB-INF/sample.tld"prefix="t" %>
Today Date and Time : <t:today/>
packagecom.first;
importjava.util.Calendar;
importjavax.servlet.jsp.JspException;
importjavax.servlet.jsp.JspWriter;
importjavax.servlet.jsp.tagext.TagSupport;
publicclasstagHandlerextendsTagSupport{
publicintStart() throwsJspException {
JspWriterj=pageContext.getOut();
try{
j.print(Calendar.getInstance().getTime());
}catch(Exception e){System.out.println(e);}
returnSKIP_BODY;
}
}
Output:
Example #2
Code:
<%@pagelanguage="java"contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@tagliburi="WEB-INF/sample.tld" prefix="c"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
<title>Welcome To My Domain</title>
</head>
<body>
<c:setvar="demo"value="Welcome To My Domain"></c:set>
<c:outvalue="${demo}"></c:out>
</body>
</html>
Output :
Example #3
Code :
<%@pagelanguage="java"contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%>
<%@tagliburi="http://java.sun.com/jsp/jstl/functions"prefix="fn" %>
<html>
<head>
<title>Sample</title>
</head>
<body>
<c:setvar="s"value="Welcome"/>
<c:setvar="s1"value="To"/>
<c:setvar="s2"value="MyDomain"/>
String1 Length is: ${fn:length(s)}<br>
String2 Length is: ${fn:length(s1)}<br>
String3 Length is: ${fn:length(s2)}
</body>
</html>
Output:
The above examples show the JSTL tag libraries in three different ways. In the first example we have done the basic date and time operations using tag library directives like our own prefix with inbuilt methods and it shows the current date and time the second example shows only basic core functions in JSTL tags it just prints the string in the screen using <c:out> tag and the final example we used both core and functions of the JSTL it has to be calculated for the three string length using the jstl functions called fn: length() method whatever we initialized strings it has to be calculated the length.
Conclusion
It is denoted as tld file and it has the set of built-in functionalities used in the JSP code. In Jsp 2.0 version have the feature called simple tag support class its implemented with the SimpleTag interfaces and they used for getter methods to retrieve the properties.
Recommended Article
This is a guide to JSP Tag Library. Here we discuss the Introduction to JSP Tag Library and how it works along with its examples and Code Implementation. You can also go through our other suggested articles to learn more –