Women in Technology

Hear us Roar



Article:
  Designing JSP Custom Tag Libraries
Subject:   corrected files (including web.xml)
Date:   2003-02-14 09:59:11
From:   anonymous2
JSP
---


<%@ taglib uri="/WEB-INF/oreillySample.tld" prefix="sample" %>
<html>
<head>
<title>Your Standard Hello World Demo</title>
</head>
<body bgcolor="#ffffff">
<hr />
<sample:hello name="Sue"/>
<hr />
</body>
</html>


JAVA
----


package com.ml.gmi.edsi.ipro.client.action.testClasses;


import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
/**
* This is a simple tag example to show how content is added to the
* output stream when a tag is encountered in a JSP page.
*/
public class Hello extends TagSupport {
private String name=null;
/**
* Getter/Setter for the attribute name as defined in the tld file
* for this tag
*/
public void setName(String value){
name = value;
}


public String getName(){
return(name);
}


/**
* doStartTag is called by the JSP container when the tag is encountered
*/
public int doStartTag() {
try {
JspWriter out = pageContext.getOut();
out.println("<table border=1>");
if (name != null)
out.println("<tr><td> Hello " + name + " </td></tr>");
else
out.println("<tr><td> Hello World </td></tr>");
} catch (Exception ex) {
throw new Error("All is not well in the world.");
}
// Must return SKIP_BODY because we are not supporting a body for this
// tag.
return SKIP_BODY;
}
/**
* doEndTag is called by the JSP container when the tag is closed
*/
public int doEndTag(){
try {
JspWriter out = pageContext.getOut();
out.println("</table>");
} catch (Exception ex){
throw new Error("All is not well in the world.");
}
return EVAL_PAGE;
}
}


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>oreillySamples</shortname>
<info>OReilly Sample Tag library</info>
<tag>
<name>hello</name>
<tagclass>com.ml.gmi.edsi.ipro.client.action.testClasses.Hello</tagclass>
<bodycontent>empty</bodycontent>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>


WEB.XML
--------


<?xml version="1.0" encoding="ISO-8859-1"?>


<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">


<web-app>



<!-- Action Servlet Configuration -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>com.ml.gmi.edsi.ipro.client.action.IPROActionServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>



<!-- Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>


<taglib>
<taglib-uri>/WEB-INF/oreillySample.tld</taglib-uri>
<taglib-location>/WEB-INF/oreillySample.tld</taglib-location>
</taglib>


</web-app>