KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > taglib > MessageTag


1 package example.taglib;
2
3 import javax.servlet.jsp.*;
4 import javax.servlet.jsp.tagext.*;
5 import java.io.*;
6
7 public class MessageTag extends BodyTagSupport {
8
9   /* tag attributes fields:
10    *
11    * member variables to store the value of tag attributes are treated
12    * as read-only. Resin will set the values using setXXXX() based on
13    * the values passed as attributes to the tag, and the code in the
14    * class will never change the values.
15    */

16   private String JavaDoc _attrTitle;
17
18   /* internal tag fields
19    *
20    * Since an instance of this class can be reused, initialization of
21    * internal member variables happens in the init() method, which is
22    * called from doStartTag()
23    */

24   private String JavaDoc _title;
25   private StringBuffer JavaDoc _msg;
26   
27   public void setTitle(String JavaDoc title)
28   {
29     // this is the only place where _attrTitle is ever set
30
_attrTitle = title;
31   }
32   
33   public int doStartTag()
34     throws JspException
35   {
36     // initialize internal member variables
37
init();
38  
39     return EVAL_BODY_BUFFERED;
40   }
41
42   public int doEndTag()
43     throws JspException
44   {
45     // initialize internal member variables
46
init();
47     
48     try {
49       // print the message out
50
JspWriter out = pageContext.getOut();
51       out.println("<p>");
52       out.println("<table border=1>");
53       out.println("<tr><td>");
54       out.println("instance: " + this);
55       out.println("<tr><td>");
56       out.println(_title);
57       out.println("<tr><td>");
58       out.println(_msg.toString());
59       out.println("</table>");
60
61     } catch (Exception JavaDoc ex) {
62       throw new JspException(ex);
63     }
64
65     return EVAL_PAGE;
66   }
67
68   /**
69    * Set defaults for attributes and initialize internal member
70    * variables.
71    */

72   protected void init()
73   {
74     // default value for _title is "Default Title"
75
_title = _attrTitle;
76     if (_title == null)
77       _title = "Default Title";
78
79     // internal member variables
80
_msg = new StringBuffer JavaDoc();
81
82   }
83
84   /**
85    * called by nested children to add to the message. This happens
86    * after doStartTag() and before doEndTag()
87    */

88   void addToMessage(String JavaDoc text)
89     throws JspException
90   {
91     _msg.append(text);
92     _msg.append("<br>");
93   }
94 }
95
96
Popular Tags