Java/JSP/Tag
Содержание
- 1 A custom tag: empty
- 2 A custom tag: empty with attributes
- 3 A custom tag: iteration
- 4 A custom tag: scripting variable
- 5 A custom tag that has neither attributes nor body content.
- 6 Create your own tag: a custom tag body
- 7 JSP classic tags
- 8 JSP Directives: HTML tag
- 9 JSP Simple Tags
- 10 JSP tag: advanced tags
- 11 JSP Tag Libraries and JSTL
- 12 Logo Tag
- 13 Tag lifecycle with Attribute
- 14 Write your own tag
- 15 Your own simple JSP tag
A custom tag: empty
<!-- this must be added to the web application"s web.xml -->
<taglib>
<taglib-uri>/jexp</taglib-uri>
<taglib-location>/WEB-INF/jexp.tld</taglib-location>
</taglib>
// create File:jexp.tld in the /WEB-INF/
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.ru/dtd/web-jsptaglibrary_1_2.dtd">
<!-- a tab library descriptor -->
<taglib xmlns="http://java.sun.ru/JSP/TagLibraryDescriptor">
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>jexp Simple Tags</short-name>
<!-- a tag that outputs information when different methods are called -->
<tag>
<name>tagLifecycle</name>
<tag-class>com.jexp.TagLifecycle</tag-class>
<body-content>empty</body-content>
<attribute>
<name>attr1</name>
</attribute>
<attribute>
<name>attr2</name>
</attribute>
</tag>
</taglib>
//compile the following code into WEB-INF\classes\com\jexp
package com.jexp;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;
/* This tag handler outputs information about when different methods
are called.
*/
public class Lifecycle extends TagSupport
{
// Constructor
public Lifecycle()
{
System.out.println("constructor called");
}
// Methods inherited from TagSupport
public void setPageContext(PageContext p)
{
super.setPageContext(p);
System.out.println("setPageContext() called");
}
public void setParent(Tag t)
{
super.setParent(t);
System.out.println("setParent() called");
}
public void release()
{
System.out.println("release() called");
}
// Code to implement the "attr1" attribute
private String attr1;
public String getAttr1()
{
return attr1;
}
public void setAttr1(String s)
{
System.out.println("setAttr1() called with value " + s);
attr1 = s;
}
// Code to implement the "attr2" attribute
private String attr2;
public String getAttr2()
{
return attr2;
}
public void setAttr2(String s)
{
System.out.println("setAttr2() called with value " + s);
attr2 = s;
}
public int doStartTag() throws JspException
{
System.out.println("doStartTag() called");
return SKIP_BODY;
}
public int doEndTag() throws JspException
{
System.out.println("doEndTag() called");
return super.doEndTag();
}
}
// start comcat and load the following jsp page in browser
<%@ taglib uri="/jexp" prefix="jexp" %>
<html>
<head>
<title>A custom tag: empty</title>
</head>
<body>
<jexp:tagLifecycle attr1="Rob" attr2="G" />
<jexp:tagLifecycle attr1="Joe" attr2="S" />
Check your Tomcat console window for the output
</body>
</html>
A custom tag: empty with attributes
///
<!-- this must be added to the web application"s web.xml -->
<taglib>
<taglib-uri>/jexp</taglib-uri>
<taglib-location>/WEB-INF/jexp.tld</taglib-location>
</taglib>
// create File:jexp.tld in the /WEB-INF/
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.ru/dtd/web-jsptaglibrary_1_2.dtd">
<!-- a tab library descriptor -->
<taglib xmlns="http://java.sun.ru/JSP/TagLibraryDescriptor">
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>jexp Simple Tags</short-name>
<!-- this tag lists random numbers; the HTML is hard-coded within
the tag handler
-->
<tag>
<name>emptyTagWithAttrs</name>
<tag-class>com.jexp.EmptyTagWithAttrs</tag-class>
<body-content>empty</body-content>
<attribute>
<name>howMany</name>
</attribute>
</tag>
</taglib>
//compile the following code into WEB-INF\classes\com\jexp
package com.jexp;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/* This tag handler generates the random numbers. The "howMany" attribute
specifies how many numbers to generate and display.
*/
public class EmptyTagWithAttrs extends TagSupport
{
// Code to implement the "howMany" attribute
private int howMany;
public int getHowMany()
{
return howMany;
}
public void setHowMany(int i)
{
howMany = i;
}
public int doStartTag() throws JspException
{
try
{
JspWriter out = pageContext.getOut();
for ( int i=0; i<this.howMany; i++ )
{
int nextNumber = (int) (Math.random() * 10);
out.println("<li>" + nextNumber + "</li>");
} // end of for ()
}
catch (IOException e)
{
System.out.println("Error in EmptyTagWithAttrs.doStartTag()");
e.printStackTrace();
throw new JspException(e); // throw the error to the error page (if set)
} // end of try-catch
return SKIP_BODY;
}
}
// start comcat and load the following jsp page in browser
<%@ taglib uri="/jexp" prefix="jexp" %>
<html>
<head>
<title>A custom tag: empty with attributes</title>
</head>
<body>
This page uses a custom tag that has no body content, but takes
an attribute called "howMany" that specifies how many random
numbers to generate. Here is its output:
<ul>
<java2:emptyTagWithAttrs howMany="5" />
</ul>
</body>
</html>
A custom tag: iteration
<!-- this must be added to the web application"s web.xml -->
<taglib>
<taglib-uri>/jexp</taglib-uri>
<taglib-location>/WEB-INF/jexp.tld</taglib-location>
</taglib>
// create File:jexp.tld in the /WEB-INF/
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.ru/dtd/web-jsptaglibrary_1_2.dtd">
<!-- a tab library descriptor -->
<taglib xmlns="http://java.sun.ru/JSP/TagLibraryDescriptor">
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>jexp Simple Tags</short-name>
<!-- this tag iterates over its body a number of times -->
<tag>
<name>iterationTag</name>
<tag-class>com.jexp.IterationTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>howMany</name>
</attribute>
</tag>
</taglib>
//compile the following code into WEB-INF\classes\com\jexp
package com.jexp;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/* This tag iterates a number of times, as specified by
its "howMany" attribute.
*/
public class IterationTag extends TagSupport
{
// Code to implement the "howMany" attribute
private int howMany;
public int getHowMany()
{
return howMany;
}
public void setHowMany(int i)
{
howMany = i;
}
public int doStartTag() throws JspException
{
return EVAL_BODY_INCLUDE;
}
private int countIterations = 0;
public int doAfterBody() throws JspException
{
int retValue = (countIterations >= (howMany - 1)) ? SKIP_BODY
: EVAL_BODY_AGAIN;
try
{
JspWriter out = pageContext.getOut();
if ( countIterations < howMany)
{
int nextNumber = (int) (Math.random() * 10);
out.println(nextNumber + "<br />");
}
}
catch (IOException e)
{
System.out.println("Error in IterateTag.doAfterBody()");
e.printStackTrace();
throw new JspException(e); // throw the error to the error page (if set)
} // end of try-catch
countIterations++;
return retValue;
} // end of doAfterBody()
} // end of class IterationTag
// start comcat and load the following jsp page in browser
<%@ taglib uri="/jexp" prefix="java2" %>
<html>
<head>
<title>A custom tag: iteration</title>
</head>
<body>
output: <p />
<jexp:iterationTag howMany="5">
Here is a random number:
</jexp:iterationTag>
</body>
</html>
A custom tag: scripting variable
<!-- this must be added to the web application"s web.xml -->
<taglib>
<taglib-uri>/jexp</taglib-uri>
<taglib-location>/WEB-INF/jexp.tld</taglib-location>
</taglib>
// create File:jexp.tld in the /WEB-INF/
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.ru/dtd/web-jsptaglibrary_1_2.dtd">
<!-- a tab library descriptor -->
<taglib xmlns="http://java.sun.ru/JSP/TagLibraryDescriptor">
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>jexp Simple Tags</short-name>
<!-- this tag lists random numbers; the HTML is hard-coded within
the tag handler
-->
<!-- this tag exports random numbers in a named array -->
<tag>
<name>defineObjects</name>
<tag-class>com.conygre.jspdevhandbook.chapter9.EmptyTagWithAttrsExport</tag-class>
<tei-class>com.conygre.jspdevhandbook.chapter9.EmptyTagExtraInfo</tei-class>
<body-content>empty</body-content>
<!--
Use this block instead of a TEI class
<variable>
<name-from-attribute>name</name-from-attribute>
<variable-class>int []</variable-class>
<declare>TRUE</declare>
<scope>AT_END</scope>
</variable>
-->
<attribute>
<name>howMany</name>
</attribute>
<attribute>
<name>name</name>
</attribute>
</tag>
</taglib>
//compile the following code into WEB-INF\classes\com\jexp
package com.jexp;
import javax.servlet.jsp.tagext.TagData;
import javax.servlet.jsp.tagext.TagExtraInfo;
import javax.servlet.jsp.tagext.VariableInfo;
public class EmptyTagExtraInfo extends TagExtraInfo
{
public VariableInfo[] getVariableInfo(TagData tagData)
{
String exportedArrayName = (String) tagData.getAttribute("name");
VariableInfo exportedArrayInfo = new VariableInfo(exportedArrayName,
"int []",
true,
VariableInfo.AT_END);
return new VariableInfo[] {exportedArrayInfo};
}
}
package com.jexp;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
/* This tag handler generates the random numbers. The "howMany" attribute
specifies how many number to generate and display. The generated array
of integers is exported with nested visibility, through an array named
byte the "name" attribute.
*/
public class EmptyTagWithAttrsExport extends BodyTagSupport
{
// Code to implement the "howMany" attribute
private int howMany;
public int getHowMany()
{
return howMany;
}
public void setHowMany(int i)
{
howMany = i;
}
// Code to implement the "name" attribute
private String exportedArrayName;
public String getName()
{
return exportedArrayName;
}
public void setName(String s)
{
exportedArrayName = s;
}
public int doStartTag() throws JspException
{
int[] outputArray = new int[howMany];
System.out.println("Generating " + howMany + " numbers");
for ( int i=0; i<howMany; i++ )
{
outputArray[i] = (int) (Math.random() * 10);
} // end of for ()
pageContext.setAttribute(exportedArrayName, outputArray);
return SKIP_BODY;
}
/* public int doEndTag() throws JspException
{
return super.doEndTag();
}*/
}
// start comcat and load the following jsp page in browser
<%@ taglib uri="/jexp" prefix="jexp" %>
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
<head>
<title>A custom tag: scripting variable</title>
</head>
<body>
output:
<jexp:defineObjects howMany="5" name="numbers" />
<ul>
<c:forEach items="${numbers}" var="currentNumber">
<li>
<c:out value="${currentNumber}" />
</li>
</c:forEach>
</ul>
</body>
</html>
A custom tag that has neither attributes nor body content.
/// Empty Tag
<!-- this must be added to the web application"s web.xml -->
<taglib>
<taglib-uri>/jexp</taglib-uri>
<taglib-location>/WEB-INF/jexp.tld</taglib-location>
</taglib>
// create File:jexp.tld in the /WEB-INF/
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.ru/dtd/web-jsptaglibrary_1_2.dtd">
<!-- a tab library descriptor -->
<taglib xmlns="http://java.sun.ru/JSP/TagLibraryDescriptor">
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>jexp Simple Tags</short-name>
<!-- this tag just outputs some text -->
<tag>
<name>emptyTag</name>
<tag-class>com.jexp.EmptyTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
//compile the following code into WEB-INF\classes\com\jexp
package com.jexp;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class EmptyTag extends TagSupport
{
public int doStartTag() throws JspException
{
try
{
pageContext.getOut().print("in EmptyTag.doStartTag()");
}
catch (IOException e)
{
System.out.println("Error in EmptyTag.doStartTag()");
e.printStackTrace();
throw new JspException(e); // throw the error to the error page (if set)
} // end of try-catch
return SKIP_BODY;
}
}
// start comcat and load the following jsp page in browser
<%@ taglib uri="/jexp" prefix="jexp" %>
<html>
<head>
<title>A custom tag: empty</title>
</head>
<body>
This page uses a custom tag that has neither attributes nor body content.
Here is its output:
<h1><jexp:emptyTag /></h1>
</body>
</html>
Create your own tag: a custom tag body
///
<!-- this must be added to the web application"s web.xml -->
<taglib>
<taglib-uri>/jexp</taglib-uri>
<taglib-location>/WEB-INF/jexp.tld</taglib-location>
</taglib>
// create File:jexp.tld in the /WEB-INF/
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.ru/dtd/web-jsptaglibrary_1_2.dtd">
<!-- a tab library descriptor -->
<taglib xmlns="http://java.sun.ru/JSP/TagLibraryDescriptor">
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>jexp Simple Tags</short-name>
<!-- this tag manipulates its body content by converting it to upper case
-->
<tag>
<name>bodyContentTag</name>
<tag-class>com.jexp.BodyContentTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>howMany</name>
</attribute>
</tag>
</taglib>
//compile the following code into WEB-INF\classes\com\jexp
package com.jexp;
import java.io.IOException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class BodyContentTag extends BodyTagSupport
{
private int iterations, howMany;
public void setHowMany(int i)
{
this.howMany = i;
}
public void setBodyContent(BodyContent bc)
{
super.setBodyContent(bc);
System.out.println("BodyContent = "" + bc.getString() + """);
}
public int doAfterBody()
{
try
{
BodyContent bodyContent = super.getBodyContent();
String bodyString = bodyContent.getString();
JspWriter out = bodyContent.getEnclosingWriter();
if ( iterations % 2 == 0 )
out.print(bodyString.toLowerCase());
else
out.print(bodyString.toUpperCase());
iterations++;
bodyContent.clear(); // empty buffer for next evaluation
}
catch (IOException e)
{
System.out.println("Error in BodyContentTag.doAfterBody()" + e.getMessage());
e.printStackTrace();
} // end of catch
int retValue = SKIP_BODY;
if ( iterations < howMany )
retValue = EVAL_BODY_AGAIN;
return retValue;
}
}
// start comcat and load the bodyContent.jsp in browser
<%@ taglib uri="/jexp" prefix="jexp" %>
<html>
<head>
<title>A custom tag: body content</title>
</head>
<body>
This page uses a custom tag manipulates its body content.
Here is its output:
<ol>
<jexp:bodyContentTag howMany="3">
<li>jexp.ru</li>
</jexp:bodyContentTag>
</ol>
</body>
</html>
JSP classic tags
JSP Directives: HTML tag
/*
Beginning JavaServer Pages
Vivek Chopra, Jon Eaves, Rupert Jones, Sing Li, John T. Bell
ISBN: 0-7645-7485-X
*/
JSP Simple Tags
///
<!-- this must be added to the web application"s web.xml -->
<taglib>
<taglib-uri>/jexp</taglib-uri>
<taglib-location>/WEB-INF/jexp.tld</taglib-location>
</taglib>
// create File:jexp.tld in the /WEB-INF/
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.ru/dtd/web-jsptaglibrary_1_2.dtd">
<!-- a tab library descriptor -->
<taglib xmlns="http://java.sun.ru/JSP/TagLibraryDescriptor">
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>jexp Simple Tags</short-name>
<!-- Expression Language function -->
<function>
<name>ReverseString</name>
<function-class>com.jexp.ELFunctions</function-class>
<function-signature>String reverse(String)</function-signature>
</function>
<!-- end of Expression Language function -->
</taglib>
//compile the following code into WEB-INF\classes\com\jexp
package com.jexp;
public class ELFunctions
{
public static String reverse(String param)
{
return new StringBuffer(param).reverse().toString();
}
}
// start comcat and load the following jsp page in browser
<%@ taglib uri="/jexp" prefix="jexp" %>
<html>
<head>
<title>An Expression Language Tag</title>
</head>
<body>
output:
<h1>${jexp:ReverseString("Hello World from www.jexp.ru!")}</h1>
</body>
</html>
JSP tag: advanced tags
JSP Tag Libraries and JSTL
/*
Beginning JavaServer Pages
Vivek Chopra, Jon Eaves, Rupert Jones, Sing Li, John T. Bell
ISBN: 0-7645-7485-X
*/
Logo Tag
package com.jexp;
///Custom tag
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.TryCatchFinally;
/**
* This tag generates a thumbnail image using HTML img tag, next to text
* message. The user specifies the content of the message and the Heading level
* (i.e.,
* <H1>-
* <H2>)
*/
public class LogoTag extends BodyTagSupport implements TryCatchFinally {
private String heading = null;
private String image = null;
//stamp.gif, 42 x 54
private String width = null;
private String height = null;
public int doStartTag() throws JspException {
//this method assumes that attribute properties have been set.
try {
int h = new Integer(heading).intValue();
if (!(h > 0 && h < 7))
throw new JspException(
"The "heading" attribute value must between 1 and 6 inclusive.");
} catch (Exception e) {
throw new JspException(e.getMessage());
}
return EVAL_BODY_BUFFERED;
}
public int doEndTag() throws JspException {
JspWriter out = pageContext.getOut();
String imgDir = ((HttpServletRequest) pageContext.getRequest())
.getContextPath()
+ "/images/";
String message = getBodyContent().getString().trim();
try {
out.println(new StringBuffer("<img src=\"").append(imgDir).append(
image).append("\" width=\"").append(width).append(
"\" height=\"").append(height).append("\" align=\"left\">")
.append("<H").append(heading).append(">").append(message)
.append("</H").append(heading).append(">").toString());
} catch (java.io.IOException io) {
}
return EVAL_PAGE;
}
public void doCatch(Throwable t) {
try {
pageContext.getOut().println(t.getMessage() + "<br />");
} catch (java.io.IOException io) {
}
}
public void doFinally() {
//do nothing here, since we don"t have any resources open
//like database connections
}
public void setHeading(String level) {
this.heading = level;
}
public void setImage(String name) {
this.image = name;
}
public void setWidth(String width) {
this.width = width;
}
public void setHeight(String height) {
this.height = height;
}
public void release() {
heading = null;
image = null;
width = null;
height = null;
}
}
//
package com.jexp;
import java.io.IOException;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
/**
* This tag generates a thumbnail image using HTML img tag, next to text
* message. The user specifies the content of the message and the Heading level
* (i.e.,
* <H1>-
* <H2>)
*/
public class SimpleLogoTag extends SimpleTagSupport {
private String heading = null;
private String image = null;
private String width = null;
private String height = null;
public void doTag() throws JspException, IOException {
JspContext jspContext = getJspContext();
//this method assumes that attribute properties have been set.
try {
int h = new Integer(heading).intValue();
if (!(h > 0 && h < 7))
throw new JspException(
"The "heading" attribute value must between 1 and 6 inclusive.");
} catch (Exception e) {
throw new JspException(e.getMessage());
}
JspWriter out = jspContext.getOut();
String imgDir = (String) jspContext.findAttribute("imgDir");
if (imgDir == null || "".equals(imgDir))
throw new JspException(
"No attribute provided specifying the application"s image directory.");
out.println(new StringBuffer("<img src=\"").append(imgDir)
.append(image).append("\" width=\"").append(width).append(
"\" height=\"").append(height).append(
"\" align=\"left\">").append("<H").append(heading)
.append(">").toString());
getJspBody().invoke(null);
out.println(new StringBuffer("</H").append(heading).append(">")
.toString());
}
public void setHeading(String level) {
this.heading = level;
}
public void setImage(String name) {
this.image = name;
}
public void setWidth(String width) {
this.width = width;
}
public void setHeight(String height) {
this.height = height;
}
}
//logo.tag
<%@ tag body-content="scriptless" description="Writes the HTML code for inserting a logo." %>
<%@ taglib prefix="c" uri="http://java.sun.ru/jsp/jstl/core" %>
<%@ attribute name="heading" required="true" rtexprvalue=
"true" description="The heading level for the logo."%>
<%@ attribute name="image" required="true" rtexprvalue=
"true" description="The image name for the logo."%>
<%@ attribute name="width" required="true" rtexprvalue=
"true" description="The image width for the logo."%>
<%@ attribute name="height" required="true" rtexprvalue=
"true" description="The image height for the logo."%>
<img src="${imgDir}${image}" width=
"${width}" height="${height}" align="left">
<H${heading}>
<jsp:doBody/></H${heading}>
//logoTest.jsp
<%@ taglib uri="jexp.ru.tags" prefix="cbck" %>
<html>
<head><title></title></head>
<body>
<% session.setAttribute("imgDir",(request.getContextPath() + "/images/")) %>
<cbck:logo heading="2" image="stamp.gif" width="42" height="54">Thanks for visiting</cbck:logo>
Here"s all the other stuff this page contains...
</body>
</html>
//myTag.tld
<taglib xmlns="http://java.sun.ru/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.ru/xml/ns/j2ee http://java.sun.ru/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0">
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>cbck</short-name>
<uri>jexp.ru.tags</uri>
<description>Cookbook custom tags</description>
<listener>
<listener-class>com.jexp.ReqListener</listener-class>
</listener>
<tag>
<name>logo</name>
<tag-class>com.jexp.LogoTag</tag-class>
<body-content>scriptless</body-content>
<description>This tag writes a logo inside the JSP.</description>
<attribute>
<name>heading</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<description>The heading level for the logo; 1 through 6.</description>
</attribute>
<attribute>
<name>image</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<description>The image name for the logo.</description>
</attribute>
<attribute>
<name>width</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<description>The image width for the logo.</description>
</attribute>
<attribute>
<name>height</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<description>The image height for the logo.</description>
</attribute>
</tag>
</taglib>
Tag lifecycle with Attribute
<!-- this must be added to the web application"s web.xml -->
<taglib>
<taglib-uri>/jexp</taglib-uri>
<taglib-location>/WEB-INF/jexp.tld</taglib-location>
</taglib>
// create File:jexp.tld in the /WEB-INF/
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.ru/dtd/web-jsptaglibrary_1_2.dtd">
<!-- a tab library descriptor -->
<taglib xmlns="http://java.sun.ru/JSP/TagLibraryDescriptor">
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>jexp Simple Tags</short-name>
<!-- a tag that outputs information when different methods are called -->
<tag>
<name>tagLifecycle</name>
<tag-class>com.jexp.TagLifecycle</tag-class>
<body-content>empty</body-content>
<attribute>
<name>attr1</name>
</attribute>
<attribute>
<name>attr2</name>
</attribute>
</tag>
</taglib>
//compile the following code into WEB-INF\classes\com\jexp
package com.jexp;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;
/* This tag handler outputs information about when different methods
are called in the Tag interface.
*/
public class TagLifecycle extends TagSupport
{
// Constructor
public TagLifecycle()
{
System.out.println("TagLifecycle constructor called");
}
// Methods inherited from TagSupport
public void setPageContext(PageContext p)
{
super.setPageContext(p);
System.out.println("setPageContext() called (" + p + ")");
}
public void setParent(Tag t)
{
super.setParent(t);
System.out.println("setParent() called (" + t + ")");
}
public Tag getParent()
{
System.out.println("getParent() called");
return super.getParent();
}
public void release()
{
System.out.println("release() called");
}
// Code to implement the "attr1" attribute
private String attr1;
public String getAttr1()
{
return attr1;
}
public void setAttr1(String s)
{
System.out.println("setAttr1() called with value " + s);
attr1 = s;
}
// Code to implement the "attr2" attribute
private String attr2;
public String getAttr2()
{
return attr2;
}
public void setAttr2(String s)
{
System.out.println("setAttr2() called with value " + s);
attr2 = s;
}
public int doStartTag() throws JspException
{
System.out.println("doStartTag() called");
return SKIP_BODY;
}
public int doEndTag() throws JspException
{
System.out.println("doEndTag() called");
return super.doEndTag();
}
}
// start comcat and load the following jsp page in browser
<%@ taglib uri="/jexp" prefix="jexp" %>
<html>
<head>
<title>The lifecycle of the Tag interface</title>
</head>
<body>
<jexp:tagLifecycle attr1="Joe" attr2="Greenhough" />
<jexp:tagLifecycle attr1="Rob" attr2="Greenhough" />
Check the Tomcat console for the output
</body>
</html>
Write your own tag
// Sample tld file
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.ru/dtd/web-jsptaglibrary_1_2.dtd">
<!-- a tab library descriptor -->
<taglib xmlns="http://java.sun.ru/JSP/TagLibraryDescriptor">
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>Mark"s Simple Tags</short-name>
<tag>
<name>HelloWorldTag</name>
<tag-class>test.HelloWorldTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
// Sample JSP file
<%@ taglib uri="webapp/META-INF/simple.tld" prefix="hw" %>
<html>
<body>
<hw:HelloWorldTag />
</body>
</html>
// Sample Java file
package test;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;
public class HelloWorldTag extends TagSupport
{
public int doStartTag() throws JspException
{
try
{
JspWriter out = pageContext.getOut();
HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
out.write("Hello world!");
}
catch(Exception e)
{
throw new JspException(e.getMessage());
}
return EVAL_PAGE;
}
}
Your own simple JSP tag
//jexpYourOwnJSPTag.war
// add to web.xml
<taglib>
<taglib-uri>http://java.sun.ru/jstl/jexp</taglib-uri>
<taglib-location>/WEB-INF/jexp.tld</taglib-location>
</taglib>
// create new jexp.tld file in WEB-INF directory
//File:jexp.tld
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.ru/dtd/web-jsptaglibrary_1_2.dtd">
<!-- a tab library descriptor -->
<taglib xmlns="http://java.sun.ru/JSP/TagLibraryDescriptor">
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>jexp Simple Tags</short-name>
<tag>
<name>HelloWorldTag</name>
<tag-class>com.jexp.HelloWorldTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
// create HelloWorldTag.java under WEB-INF\classes\com\jexp\
// include jsp-api.jar into your CLASSPATH
// and compile it
package com.jexp;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;
public class HelloWorldTag extends TagSupport
{
public int doStartTag() throws JspException
{
try
{
JspWriter out = pageContext.getOut();
HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
out.write("Hello world from www.jexp.ru!");
}
catch(Exception e)
{
throw new JspException(e.getMessage());
}
return EVAL_PAGE;
}
}
// create JSP file: HelloWorldTag.jsp
<%@ taglib uri="WEB-INF/jexp.tld" prefix="hw" %>
<!-- the HellowWorld must be consistent with the tag name in *.tld-->
<html>
<body>
<hw:HelloWorldTag />
</body>
</html>
// start tomcat and load the JSP file in the browser