View Javadoc

1   /*
2   * Copyright 2004 The Apache Software Foundation
3   *
4   * Licensed under the Apache License, Version 2.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7   *
8   *     http://www.apache.org/licenses/LICENSE-2.0
9   *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16  package examples;
17  
18  import javax.servlet.jsp.*;
19  import javax.servlet.jsp.tagext.*;
20  import java.util.Hashtable;
21  import java.io.Writer;
22  import java.io.IOException;
23  
24  /**
25   * Example1: the simplest tag
26   * Collect attributes and call into some actions
27   *
28   * <foo att1="..." att2="...." att3="...." />
29   */
30  
31  public class FooTag 
32      extends ExampleTagBase 
33  {
34      private String atts[] = new String[3];
35      int i = 0;
36      
37      private final void setAtt(int index, String value) {
38          atts[index] = value;
39      }
40      
41      public void setAtt1(String value) {
42          setAtt(0, value);
43      }
44      
45      public void setAtt2(String value) {
46          setAtt(1, value);
47      }
48  
49      public void setAtt3(String value) {
50          setAtt(2, value);
51      }
52      
53      /**
54       * Process start tag
55       *
56       * @return EVAL_BODY_INCLUDE
57       */
58      public int doStartTag() throws JspException {
59          i = 0;
60  	return EVAL_BODY_TAG;
61      }
62  
63      public void doInitBody() throws JspException {
64          pageContext.setAttribute("member", atts[i]);
65          i++;
66      }
67      
68      public int doAfterBody() throws JspException {
69          try {
70              if (i == 3) {
71                  bodyOut.writeOut(bodyOut.getEnclosingWriter());
72                  return SKIP_BODY;
73              } else
74                  pageContext.setAttribute("member", atts[i]);
75              i++;
76              return EVAL_BODY_TAG;
77          } catch (IOException ex) {
78              throw new JspTagException(ex.toString());
79          }
80      }
81  }
82