/* * codeconv.GetResultsTag * * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. * * This software is the proprietary information of Sun Microsystems, Inc. * Use is subject to license terms. * * Version: @(#) * * Date: January 13, 2003 * */ package codeconv; import java.util.Date; import java.util.Random; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.TagSupport; /** * Retrieves fake lottery results for the given date and returns the * result in the given attribute * * @version @(#) * @author Mark Roth */ public class GetResultsTag extends TagSupport { /** The maximum number that can be drawn */ private static final int MAX_NUMBER = 58; /** The number of numbers that are drawn */ private static final int NUMBER_COUNT = 6; /** The variable to store results to */ private String var; /** The date on which the numbers were drawn */ private Date date; /** The PageContext, used to return the result */ private PageContext pageContext; /** * Remember the PageContext so it can be used later * to return the result. * * @param pageContext The page context of the calling page */ public void setPageContext( PageContext pageContext ) { this.pageContext = pageContext; } /** * Returns fake lottery results for the given date. * * @return EVAL_PAGE so the rest of the page is evaluated. */ public int doEndTag() throws JspException { // Generate the (fake) results. Random random = new Random( this.date.getTime() ); int[] result = new int[NUMBER_COUNT]; for( int i = 0; i < NUMBER_COUNT; i++ ) { result[i] = random.nextInt( MAX_NUMBER ) + 1; } // Place the result in the scoped attribute named by 'var'. pageContext.setAttribute( this.var, result ); return EVAL_PAGE; } /** * Setter for date * * @param date The fake date on which the lottery numbers were drawn */ public void setDate( Date date ) { this.date = date; } /** * Getter for date * * @return The fake date on which the lottery numbers were drawn */ public Date getDate() { return this.date; } /** * Setter for var * * @param var The name of the variable to store the result in */ public void setVar( String var ) { this.var = var; } /** * Getter for var * * @return The name of the variable to restore the result in */ public String getVar() { return this.var; } }