NOTE:
These slides have not been updated since 2003. They have been superseded by the book
Anders Møller and Michael Schwartzbach, February 2006 |
INTERACTIVE WEB SERVICES WITH JAVA |
Example JSP page with custom tag:
<%@ taglib uri="/WEB-INF/tlds/mytags.tld" prefix="my" %> <my:wrapper style="k00l"> <b>hello!</b> </my:wrapper> |
Tag Handler code (compile and put in WEB-INF/classes/mytaglib/WrapperTag.class):
package mytaglib; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; public class WrapperTag extends TagSupport { private String style; public void setStyle(String style) { this.style = style; } public int doStartTag() { try { JspWriter out = pageContext.getOut(); if (style.equals("k00l")) { out.print("<html><head><title>MyCoolService</title></head><body bgcolor=\"red\">"); } else { // ... } } catch (IOException e) { System.out.println("Error in WrapperTag: "+e); } return EVAL_BODY_INCLUDE; } public int doEndTag() { try { JspWriter out = pageContext.getOut(); out.print("</body></html>"); } catch (IOException e) { System.out.println("Error in WrapperTag: "+e); } return EVAL_PAGE; } } |
(See the API.)
Tag Library Descriptor file (put in WEB-INF/tlds/mytags.tld):
<?xml version="1.0" encoding="ISO-8859-1" ?> <!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>MyTags</short-name> <tag> <name>wrapper</name> <tag-class>mytaglib.WrapperTag</tag-class> <body-content>JSP</body-content> <attribute> <name>style</name> <required>true</required> </attribute> </tag> </taglib> |
There are lots of free tag libraries!
COPYRIGHT © 2002-2003 ANDERS MØLLER & MICHAEL I. SCHWARTZBACH |