tw.com.javaworld.CH16.DynamicAdd (Java2HTML)

| DynamicAdd.java |

    1 package tw.com.javaworld.CH16;
    2
    3 import java.io.*;
    4 import java.util.*;
    5 import javax.servlet.jsp.*;
    6 import javax.servlet.jsp.tagext.*;
    7
    8 public class DynamicAdd extends SimpleTagSupport implements DynamicAttributes {
    9
    10    private ArrayList keys = new ArrayList();
    11    private ArrayList values = new ArrayList();
    12
    13    public void doTag() throws JspException, IOException {
    14
    15        JspContext ctx = getJspContext();
    16        JspWriter out = ctx.getOut();
    17
    18        float num = 0;
    19        float sum = Float.parseFloat((String)values.get(0));
    20        out.print(sum);
    21
    22        for (int i = 1 ; i < keys.size() ; i++) {
    23            String temp = (String)values.get(i);
    24            num = Float.parseFloat(temp);
    25            sum = sum + num;
    26            out.print(" + " + num);
    27        }
    28
    29        out.print(" = " + sum);
    30        ctx.setAttribute("sum", Float.toString(sum));
    31
    32    }
    33
    34    public void setDynamicAttribute(String uri, String name, Object value) throws JspException {
    35        keys.add(name);
    36        values.add(value);
    37    }
    38}