TECHNICALFACILITATION.COM - THE ULTIMATE WEBSPHERE AND JAVA CERTIFICATION GUIDES AND RESOURCES
Google
Download the Completed Solution: 07CustomTags.ear.

Scroll to the bottom of the page for pertinent code snippets.

Creating Your Own Custom JSP Tags

This free, multimedia tutorial shows you how to use IBM's Rational Application Developer (IRAD) 6.0 to create a custom tag library (well, one custom tag, so it is a pretty impoverished library), and the corresponding Java code and tld file to make a custom tag work on a JSP page.

If you found something helpful here, please do your part and help support the site. Link to us, buy some books, support our sponsors, tell your developer friends about us, and remember: Happy Java!


package com.examscam.servlet.tags;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.examscam.common.Timer;


public class TimerTag extends TagSupport {


public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
ServletRequest request = pageContext.getRequest();
ServletResponse response = pageContext.getResponse();
ServletContext application = pageContext.getServletContext();
HttpSession session = pageContext.getSession();

Timer t = (Timer)session.getAttribute("timer");
if (t==null){
t = new Timer();
session.setAttribute("timer", t);
}
try {
out.print("<BR/><B>Elapsed Time: </B>" + t.getElapsedTime());
out.print("<BR/><I>Start Time: </I>" + t.getStartTime());

} catch (IOException e) {

e.printStackTrace();
}

return SKIP_BODY;

}
}

<?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>examscam</shortname>
<uri>/WEB-INF/examscam-taglib.tld</uri>
<info>ExamScam Tag Library</info>
<tag>
<name>timerDisplay</name>
<tagclass>com.examscam.servlet.tags.TimerTag</tagclass>
<bodycontent>empty</bodycontent>
<info>Displays Elapsed Time and Start Time</info>
</tag>
</taglib>

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>
ExamScamWeb</display-name>
<servlet>
<description>
</description>
<display-name>
CountrySnooper</display-name>
<servlet-name>CountrySnooper</servlet-name>
<servlet-class>
com.examscam.servlet.CountrySnooper</servlet-class>
</servlet>
<servlet>
<description>
</description>
<display-name>
SessionServlet</display-name>
<servlet-name>SessionServlet</servlet-name>
<servlet-class>
com.examscam.servlet.SessionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CountrySnooper</servlet-name>
<url-pattern>/CountrySnooper</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SessionServlet</servlet-name>
<url-pattern>/SessionServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/examscam-taglib.tld</taglib-uri>
<taglib-location>/WEB-INF/examscam-taglib.tld</taglib-location>
</taglib>
</jsp-config>



</web-app>





<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<HTML>
<HEAD>
<%@taglib uri="/WEB-INF/examscam-taglib.tld" prefix="examscam"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<TITLE>timertester.jsp</TITLE>
</HEAD>
<BODY>
<jsp:useBean class="com.examscam.common.Timer" id="timer" scope="session"></jsp:useBean>
<P></P>
<P>Here is the output of our timer:</P>
<BR>Elapsed Time:
<jsp:getProperty name="timer" property="elapsedTime" />
<BR>Start Time:
<jsp:getProperty name="timer" property="startTime" />
<P><BR>
<BR>Here's the output from the tag library:<BR>
<BR>
<examscam:timerDisplay/>
</P>
<P></P>
</BODY>
</HTML>
Google
THE ULTIMATE CERTIFICATION AND WEBSPHERE RESOURCES - BUY THEM NOW ON AMAZON
eXTReMe Tracker