KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > request > ParametersTag


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.request;
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>parameters</b>, used to get HttpServletRequest parameter 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>parameters</b> tag.
31  * <p>
32  * Loops through all the parameters received by the HttpServletRequest.
33  * <p>
34  * If the optional attribute <b>name</b> is present only the parameter of
35  * that name is retreived.
36  * <p>
37  * JSP Tag Lib Descriptor
38  * <p><pre>
39  * &lt;name&gt;parameters&lt;/name&gt;
40  * &lt;tagclass&gt;org.apache.taglibs.request.ParametersTag&lt;/tagclass&gt;
41  * &lt;teiclass&gt;org.apache.taglibs.request.ParametersTEI&lt;/teiclass&gt;
42  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
43  * &lt;info&gt;Used to loop through all parameters or get a single parameter.&lt;/info&gt;
44  * &lt;attribute&gt;
45  * &lt;name&gt;id&lt;/name&gt;
46  * &lt;required&gt;true&lt;/required&gt;
47  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
48  * &lt;/attribute&gt;
49  * &lt;attribute&gt;
50  * &lt;name&gt;name&lt;/name&gt;
51  * &lt;required&gt;false&lt;/required&gt;
52  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
53  * &lt;/attribute&gt;
54  * </pre>
55  *
56  * @author Glenn Nielsen
57  */

58
59 public class ParametersTag extends BodyTagSupport
60 {
61     // Optional attribute, HTTP input parameter name
62
private String JavaDoc name = null;
63     private HttpServletRequest req = null;
64     // All the parameter names
65
private Enumeration parameters = null;
66     // Name of current parameter
67
private String JavaDoc parameter = null;
68
69     /**
70      * Gets the parameters that came with the request or parameter with <b>name</b>.
71      *
72      * @return SKIP_BODY if no parameters or parameter with <b>name</b> is not found, EVAL_BODY_TAG if parameter exists
73      */

74     public final int doStartTag() throws JspException
75     {
76     // Get the request
77
req = (HttpServletRequest)pageContext.getRequest();
78
79     if( name != null ) {
80         if( req.getParameter(name) != null )
81         parameter = name;
82     } else {
83         parameters = req.getParameterNames();
84         if( parameters == null || !parameters.hasMoreElements() )
85         return SKIP_BODY;
86         parameter = (String JavaDoc)parameters.nextElement();
87     }
88     if( parameter == null )
89         return SKIP_BODY;
90
91     pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE);
92     return EVAL_BODY_TAG;
93     }
94
95     /**
96      * Method called at end of each parameters tag.
97      *
98      * @return EVAL_BODY_TAG if there is another parameter, or SKIP_BODY if there are no more parameters or this is a named parameter
99      */

100     public final int doAfterBody() throws JspException
101     {
102     // See if this is the last or a named parameter
103
if( name != null || !parameters.hasMoreElements() )
104         return SKIP_BODY;
105     // There is another parameter, so loop again
106
parameter = (String JavaDoc)parameters.nextElement();
107     if( parameter == null )
108         return SKIP_BODY;
109     return EVAL_BODY_TAG;
110     }
111
112     /**
113      * Method called at end of Tag
114      * @return EVAL_PAGE
115      */

116     public final int doEndTag() throws JspException
117     {
118         pageContext.removeAttribute(id,PageContext.PAGE_SCOPE);
119     try
120     {
121         if(bodyContent != null)
122         bodyContent.writeOut(bodyContent.getEnclosingWriter());
123     } catch(java.io.IOException JavaDoc e)
124     {
125         throw new JspException("IO Error: " + e.getMessage());
126     }
127     return EVAL_PAGE;
128     }
129
130     /**
131      * Set the optional tag attribute <b>name</b>.
132      *
133      * @param String name of HTTP input parameter
134      */

135     public final void setName(String JavaDoc str)
136     {
137     name = str;
138     }
139
140     /**
141      * Returns the name of the parameter.
142      * <p>
143      * &lt;jsp:getProperty name=<i>"id"</i> property="name"/&gt;
144      *
145      * @return String - parameter name
146      */

147     public final String JavaDoc getName()
148     {
149     return parameter;
150     }
151
152     /**
153      * Returns the value of the parameter.
154      * <p>
155      * &lt;jsp:getProperty name=<i>"id"</i> property="value"/&gt;
156      *
157      * @return String - value of the parameter
158      */

159     public final String JavaDoc getValue()
160     {
161     String JavaDoc value = req.getParameter(parameter);
162     if( value == null )
163         return "";
164     return value;
165     }
166
167 }
168
Popular Tags