Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Sunday, December 2, 2007

java code for A custom tag: empty with attributes

///



/java2s
/WEB-INF/java2s.tld


// 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">



1.0
1.2
Java2s Simple Tags



emptyTagWithAttrs
com.java2s.EmptyTagWithAttrs
empty

howMany



//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("
  • " + nextNumber + "
  • ");
    } // 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" %>



    A custom tag: empty with attributes


    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: