31 Deploying and Using Custom Tags

Pravin Jain

epgp books

 

Introduction

 

In the last module, we have seen what is a custom tag, how do we develop a custom tag, we have seen that there were three kinds of custom tags, we have a simple tag, we had a iteration tag and we also have a body tag.

 

In this module we just want to see, how do we develop this kind of tags and what is required for deploying them in a web application. Towards the end in the last module we had also seen that we should be creating some kind of a TLD file. We will see, how do we create a TLD file what kind of content goes in a TLD file, and we will also develop a couple of tags.

 

We will be developing the tag for getting the current date and time in a JSP. So, in any JSP if I want current date and time, I should be able to show the current date and time of the server, and then we might require formatting, so, we will see how we can do that. Another tag which will be used as a iteration tag., we can have some kind of a count for a ForTag. So, we will be creating two tags.

 

NowTag and

ForTag

 

The idea is to demonstrate, how do we develop custom tag, and use it in a web application, what is required for deploying it in a web application

 

Demo Of creating Custom Tags 

 

In this demo we will develop two custom tags, one is a simple tag, which can be used for showing the current date and time of the server and another tag is a iteration tag, which can be used to loop a number of times specified by a count attribute. We will than create a tld file so that these tags can be used in a JSP page, and then we also develop a small JSP file to show how the tags can be included in a JSP and used.

 

Developing the NowTag class 

 

The NowTag class is an implementation of the Tag interface. It has an attribute for the formatting the date and time. This is developed as an empty tag, and the doEndTag method is used for generating the otuput according the format attribute.

 

The code for NowTag class is as follows:

Code for NowTag class

 

package org.epgpathshala.dad.tag;

import javax.servlet.*;

import javax.servlet.jsp.*;

import javax.servlet.jsp.tagext.*;

import java.io.IOException;

import java.util.Date;

import java.text.SimpleDateFormat;

public class NowTag implements Tag { private PageContext pageContext; private Tag parent;

private String format = “dd/MM/yyyy”;

 

public void setPageContext(PageContext pc) { this.pageContext = pc;

}

public void setParent(Tag p) { this.parent = p;

}

public Tag getParent() {

return this.parent;

}

public void setFormat(String fmt) { this.format = fmt;

}

public int doStartTag() {

return Tag.SKIP_BODY;

}

public int doEndTag() { Date d = new Date();

SimpleDateFormat sdf = new SimpleDateFormat(format); try {

pageContext.getOut().println(sdf.format(d));

} catch(IOException ioe) {

ioe.printStackTrace();

}

return Tag.EVAL_PAGE;

}

public void release() {

 

}

}

Developing the For Tag class 

 

The ForTag class is an implementation of the IterationTag interface. It has an attribute for the number of times the loop should be done. This is developed to have a body, and the doAfterBody method decrements the count and checks for zero.

 

The code for ForTag class is as follows: Code for ForTag class

 

package org.epgpathshala.dad.tag;

 

import javax.servlet.*;

import javax.servlet.jsp.*;

import javax.servlet.jsp.tagext.*;

import java.io.IOException;

 

public class ForTag implements IterationTag { private PageContext pageContext;

private Tag parent;

private int count;

public void setPageContext(PageContext ctx) { this.pageContext = ctx;

}

public void setParent(Tag p) {

this.parent = p;

}

public Tag getParent() { return this.parent;

}

public void setCount(int c) {

this.count = c;

}

public int doStartTag() {

return count<=0?Tag.SKIP_BODY:Tag.EVAL_BODY_INCLUDE;

}

public int doAfterBody() {

count–;

return count<=0?Tag.SKIP_BODY:IterationTag.EVAL_BODY_AGAIN;

}

public int doEndTag() {

return Tag.EVAL_PAGE;

}

public void release() {

}

}

 

TLD file 

 

The tld file for using the two tags is as follows: Code for simple.tld

 

<taglib>

<tlib-version>1.0</tlib-version>

<jsp-version>1.2</jsp-version>

<short-name>Simple</short-name>

<tag>

<name>Now</name>

<tag-class>org.epgpathshala.dad.NowTag</tag-class>

<body-content>empty</body-content>

<attribute>

<name>format</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

</tag>

<tag>

<name>For</name>

<tag-class>org.epgpathshala.dad.ForTag</tag-class>

<body-content>jsp</body-content>

<attribute>

<name>count</name>

<required>true</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

</tag>

</taglib>

 

A sample JSP

 

The jsp file demonstrating the use of the two tags in a JSP file is as follows: Code for home.jsp

 

<%@ taglib uri=”/WEB-INF/tld/simple.tld” prefix=”simple” %>

<html><head><title>Home page</title></head>

<body>

<h1>Home Page</h1>

<simple:For count=’5′>

<h2><simple:Now format=”dd/MMM/yyyy” /></h2>

/simple:For>

</body>

you can view video on Deploying and Using Custom Tags

Suggested Reading:

  1. Core Servlets and Java Server Pages Volume 2 by Marty Hall & Larry Brown, Second Edition, Pearson Education.
  2. Core JSP by Damon Haugland and Aaron Tavistock, Pearson Education
  3. Java Server Programming for Professionals by Ivan Bayross, Sh aranam Shah, Cynhthia Bayross and Vaishali Shah Shroff Publishers and Distributros.
  4. http://docs.oracle.com/javaee/5/tutorial/doc/bnagx.html