// create File:java2s.tld in the /WEB-INF/
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
//compile the following code into WEB-INF\classes\com\java2s
package com.java2s;
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
int nextNumber = (int) (Math.random() * 10);
out.println("
} // 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="/java2s" prefix="java2s" %>
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:


No comments:
Post a Comment