Updated April 3, 2023
Introduction to JSP Taglib
Jsp(java server pages) is one of the dynamic programming languages to develop creative web pages based on Html, Xml, and some web services like Rest and Soap. The jsp have their own standard tag libraries its nothing but one of the component for java enterprise edition web applications platform. The JSP Taglib will use the jsp codes like jsp tags for use in the library specifications for some processes like xml data processing, internationalization concepts, etc. Jsp allows the vendors for creating their own custom jsp tag libraries a tag library defined as a collection of user defined actions tags will be created by the developers.
Syntax:
The basic syntax of the jsp using the tag libraries as follows.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix=”prefix" uri="WEB-INF/"%>
<html>
<body>
<% ---jsp codes ---%>
</body>
</html>
The above codes are the jsp codes use it for the custom libraries in the jsp tags.
How to Use JSP Taglib?
In jsp tag library it should be portable for the different jsp container with their own implementations. The tags of their libraries are defined and specified with the tag library descriptor file it will be used for describing the user-defined tags in the web. Using the URI the tag lib directives specifies to find where the tags are defined in the tag library descriptor file. We also declared the directives in the web.xml config file for tag libraries. The entry mentioned in the tag file specifies the usages of the tags and also where the tag attributes are exactly used and the names of those attributes.
The user-defined tags actions can be created one or more server-side instances is used for the tag libraries or using with the other jsp script elements like scriptlets also known as scripting variables. The scripting variables details and the variable instances are specified in the class called tag-extra-info class is also used to defined the custom tag usages. The tag handler concept will be used for the nested tag elements parent-child relationship mentioned in the concept for nested tags but it specified in the inner side only applicable for the programming scripts in jsp the tag handler handles the nested tag for outer side tags.
The tag handler have its own instances for java class that will be implemented for one of two important standard java interfaces it depends between the start and end side of the tags. For each and every jsp tags have their own handle classes. The tag handler instances of the server-side objects used at the specified request time period and also it has their own properties that have to be set by the jsp containers it also includes the page context instances for each jsp page that will be used in the custom tags the parent tag handler instances it will be used in the custom tag instances is nested with the outer side of the custom tag.
The custom tags will be used for some time may have body tag and sometimes its not . And also the custom tag may have the body tag it also not handle in the tags is nothing but the special handlers. It is possible for three different situations like there is no body tag that time opposed for the only start and end tag in some cases there is the body that does not need for the special handling by the tag handlers and final case is the body that needs special handling with the tag handlers.
The tag handling interfaces are described with the method called doStartTag() it will be returned to the int constant it depends upon the situations. The tag handler class implements the standard interfaces javax.servlet.jsp.tagext.Tag but in tag interface javax.servlet.jsp.tagext.TagSupport. The tag interfaces specified with the doStartTag() and doEndTag() methods the tag side developers develop the code for the methods and the tag handler class. The oracle specifies and calls the mentioned methods using Oracle JSP translator.
Examples to Implement of JSP Taglib
Below are the examples of jsp tag libraries:
Example #1
Code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
isELIgnored="false" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<%@ taglib uri="WEB-INF/tags.tld" prefix="m" %>
Today Date and Time is: <m:today/>
</body>
</html>
package com.first;
import java.util.Calendar;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class tagHandler extends TagSupport{
public int doStartTag() throws JspException {
JspWriter j=pageContext.getOut();
try{
j.print(Calendar.getInstance().getTime());
}catch(Exception e){System.out.println(e);}
return SKIP_BODY;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>tags</short-name>
<uri>http://tomcat.apache.org/example-taglib</uri>
<tag>
<name>today</name>
<tag-class>com.first.tagHandler</tag-class>
</tag>
</taglib>
Output:
Example #2
Code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
isELIgnored="false" pageEncoding="ISO-8859-1"%>
<body>
<%@ taglib uri="https://journaldev.com/jsp/tlds/tags" prefix="tags" %>
Today Date and Time is:
<tags:formatNumber number="100152.572" format="#,###.00"/><br><br>
<tags:formatNumber number="1234.5678" format="$# ###.00"/><br><br>
<p><strong>welcome To My Domain</strong></p>
</body>
public class tagHandler extends SimpleTagSupport{
private String numformat;
private String num1;
public tagHandler() {
}
public void setnumFormat(String numformat) {
this.numformat = numformat;
}
public void setNum1(String num1) {
this.num1 = num1;
}
public void doTag() throws JspException, IOException {
System.out.println("Given Number is:" + num1);
System.out.println("Number Format is:" + numformat);
try {
double d = Double.parseDouble(num1);
DecimalFormat f = new DecimalFormat(numformat);
String s = f.format(d);
getJspContext().getOut().write(s);
} catch (Exception e) {
e.printStackTrace();
throw new SkipPageException("Exception in formatting " + num1
+ " with format " + numformat);
}
}
}
Output:
Example #3
Code:
<%@ taglib uri="WEB-INF/tags.tld" prefix="welcome" %>
<welcome:To My Domain/>
<welcome:message from="siva" to="raman">Hi</welcome:message>
public class tagHandler extends SimpleTagSupport{
private String frommsg;
private String tomsg;
public void setFrommsg( final String frommsg ) {
this.frommsg = frommsg;
}
public void setTomsg( final String tomsg ) {
this.tomsg = tomsg;
}
public void doTag() throws JspException, IOException {
final StringWriter s = new StringWriter();
getJspBody().invoke( s );
getJspContext().getOut().println( "Welcome Messages'" +
frommsg + "' tomsg '" + tomsg + "'. To My Domain '" +
s.toString() + "'" );
}
}
Output:
Conclusion
Jsp custom tags and predefined tags are used as per the customer requirement also the tld file contains a single tag library with one or more tag definitions. In Jsp 2.0 version have the feature called simple tag support class these class implemented the SimpleTag interfaces and we used getter methods to retrieve the properties.
Recommended Article
This is a guide to JSP Taglib. Here we discuss the Introduction to JSP Taglib and how to use it along with its examples and Code Implementation. You can also go through our other suggested articles to learn more –