KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > session > AttributesTag


1 /*
2  * Copyright 1999,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
17 package org.apache.taglibs.session;
18
19 import java.util.*;
20 import javax.servlet.*;
21 import javax.servlet.http.*;
22 import javax.servlet.jsp.*;
23 import javax.servlet.jsp.tagext.*;
24
25 /**
26  * JSP Tag <b>attributes</b>, used to get HttpSession attribute information
27  * using the standard JSP &lt;jsp:getProperty&gt; tag.
28  * <p>
29  * The script variable of name <b>id</b> is availble only within the
30  * body of the <b>attributes</b> tag.
31  * <p>
32  * Loops through all the attributes in the HttpSession.
33  * <p>
34  * JSP Tag Lib Descriptor
35  * <p><pre>
36  * &lt;name&gt;attributes&lt;/name&gt;
37  * &lt;tagclass&gt;org.apache.taglibs.session.AttributesTag&lt;/tagclass&gt;
38  * &lt;teiclass&gt;org.apache.taglibs.session.AttributesTEI&lt;/teiclass&gt;
39  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
40  * &lt;info&gt;Used to loop through all session attributes.&lt;/info&gt;
41  * &lt;attribute&gt;
42  * &lt;name&gt;id&lt;/name&gt;
43  * &lt;required&gt;true&lt;/required&gt;
44  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
45  * &lt;/attribute&gt;
46  * </pre>
47  *
48  * @author Glenn Nielsen
49  */

50
51 public class AttributesTag extends BodyTagSupport
52 {
53     // Clients session
54
private HttpSession sess = null;
55     // All the session attributes
56
private Enumeration attributes = null;
57     // Name of the current session attribute
58
private String JavaDoc attribute = null;
59
60     /**
61      * Gets the session attributes.
62      *
63      * @return SKIP_BODY if there are no session attributes, EVAL_BODY_TAG if an attribute exists
64      */

65     public final int doStartTag() throws JspException
66     {
67     // Get the session
68
sess = pageContext.getSession();
69
70     // Loop through all session attributes
71
attributes = sess.getAttributeNames();
72     if( attributes == null || !attributes.hasMoreElements() )
73         return SKIP_BODY;
74     attribute = (String JavaDoc)attributes.nextElement();
75     if( attribute == null )
76         return SKIP_BODY;
77
78     pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE);
79     return EVAL_BODY_TAG;
80     }
81
82     /**
83      * Method called at end of each iteration of the attributes tag.
84      *
85      * @return EVAL_BODY_TAG if there is another session attribute, or SKIP_BODY if there are no more attributes
86      */

87     public final int doAfterBody() throws JspException
88     {
89     // See if this is the last or a named attribute
90
if( !attributes.hasMoreElements() )
91         return SKIP_BODY;
92     attribute = (String JavaDoc)attributes.nextElement();
93     if( attribute == null )
94         return SKIP_BODY;
95     // There is another attribute, so loop again
96
return EVAL_BODY_TAG;
97     }
98
99     /**
100      * Method called at end of attributes tag, outputs the body
101      * of the tag.
102      *
103      * @return EVAL_PAGE
104      */

105     public final int doEndTag() throws JspException
106     {
107         pageContext.removeAttribute(id,PageContext.PAGE_SCOPE);
108     try
109     {
110         if(bodyContent != null)
111         bodyContent.writeOut(bodyContent.getEnclosingWriter());
112     } catch(java.io.IOException JavaDoc e)
113     {
114         throw new JspException("IO Error: " + e.getMessage());
115     }
116     return EVAL_PAGE;
117     }
118
119     /**
120      * Returns the name of the session attribute.
121      * <p>
122      * &lt;jsp:getProperty name=<i>"id"</i> property="name"/&gt;
123      *
124      * @return String - the session attribute name
125      */

126     public final String JavaDoc getName()
127     {
128     return attribute;
129     }
130
131     /**
132      * Returns the value of the session attribute.
133      * <p>
134      * &lt;jsp:getProperty name=<i>"id"</i> property="value"/&gt;
135      *
136      * @return String - the value of the session attribute
137      */

138     public final String JavaDoc getValue()
139     {
140     Object JavaDoc value = sess.getAttribute(attribute);
141     if( value == null )
142         return "";
143     return "" + value.toString();
144     }
145
146 }
147
Popular Tags