JSP - SPLessons

Chapter 21

JSP Taglib

JSP Taglib

The JSP Taglib was introduced in JSP 1.1. These libraries allow the addition of tags similar to jsp:forward, jsp: include with different prefixes. The JSP Taglib directives are used to define tag library with a prefix that can use current JSP page. JSP Taglib might include several tag libraries. More details about tag libraries can be discussed in custom tags.

Syntax

<%@ taglib uri=“uriofthetaglibrary” prefix=“prefixoftaglibrary” %>

Here tagname will implement tag from taglib and the prefix which is given in the taglib library.

This example describes how taglib works with an example of showing date. Following are the required files to perform taglib.

index.jsp

<html>

	<head><title>Date tag example</title></head>

		<body> 
			<%@ taglib uri="http://www.splessons.com/taglib" prefix="myTag" %>
			
			<myTag:showDate/>
		</body>
</html>

In the URL just placed a link, prefix attribute informs a container what bits of markup are custom actions.
web.xml

<?xml version="1.0" encoding="UTF-8"?>
 
	<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
		<display-name>xyz</display-name>
<jsp-config>
	<taglib>
	  <taglib-uri>
             http://www.splessons.com/taglib
	  </taglib-uri>
				        
	<taglib-location>
	/WEB-INF/taglib.tld
	 </taglib-location>
	</taglib>
       </jsp-config>
	<welcome-file-list>
	 <welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	
	</web-app>
  
  

Here welcome file is used to place the starting compile page that is index.jsp.
taglib.tld

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" 
  "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">

<taglib>
	  <tlibversion>1.0</tlibversion>
	  <jspversion>1.1</jspversion>
	  <shortname>myTag</shortname>
	  <uri>http://www.splessons.com/taglib</uri>
	  <info>My own tag library</info>
	
	  <tag>
	    <name>showDate</name>
	    <tagclass>examples.ShowDateTag</tagclass>
	    <info>Show the current date</info>
	   </tag>       

</taglib>

Here mentioned versions of the JSP and Taglib.
ShowDateTag.java

package examples;

import java.io.*;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class ShowDateTag implements Tag {
	
	private PageContext pageContext;
	private Tag parent;
	
	public int doStartTag() throws JspException {
		return SKIP_BODY;
	}
	
	public int doEndTag() throws JspException {
		try {
			pageContext.getOut().write("" + new java.util.Date());
		} catch (IOException ioe) {
			throw new JspException(ioe.getMessage());
		}
		
		return EVAL_PAGE;
	}
	
	public void release() {
	}
	
	public void setPageContext(PageContext page) {
		this.pageContext = page;
	}
	
	public void setParent(Tag tag) {
		this.parent = tag;
	}
	
	public Tag getParent() {
		return this.parent;
	}
	
}

Tag handler method doStartTag() is defined by a Tag interface that is called by the Jsp page’s servlet when a start tag is evaluated. doStartTag() method is invoked when a start tag of custom tag is found. This function returns static final integer constant value that are already defined in the interface such as follows.
SKIP_BODY is an optional returned value but this value must be returned by doStartTag() when you want to skip the body evaluation that is it must be returned when the TagLibraryDescriptor file contains the element empty, the value “empty” shows that there will always be an empty action.

Deployement Descriptor

<?xml version="1.0" encoding="UTF-8"?>
 
	<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
		<display-name>xyz</display-name>
		<jsp-config>
			   <taglib>
				        <taglib-uri>
					       http://www.splessons.com/taglib
				        </taglib-uri>
				        
				        <taglib-location>
				            /WEB-INF/taglib.tld
				        </taglib-location>
			    </taglib>
		  </jsp-config>
					  <welcome-file-list>
					    <welcome-file>index.jsp</welcome-file>
					  </welcome-file-list>
	
	</web-app>
  
  

This file will be generated automatically and it is same as web.xml page.
Output

Output will be as follows .


Summary

  • JSP Taglib – To deal taglib developer has to create taglib.tld file.
  • In the place of URL developer can write anything like the name also but the same has to be modified in wex.xml and others also.
  • Deployment Descriptor file will be generated automatically file configuring.