KickJava   Java API By Example, From Geeks To Geeks.

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


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>querystrings</b>, used to get HttpServletRequest querystring
27  * parameters 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>querystrings</b> tag.
31  * <p>
32  * Loops through all the querystring parameters received by the HttpServletRequest.
33  * <p>
34  * If the optional attribute <b>name</b> is present only the querystring
35  * parameter of that name is retreived.
36  * <p>
37  * JSP Tag Lib Descriptor
38  * <p><pre>
39  * &lt;name&gt;querystrings&lt;/name&gt;
40  * &lt;tagclass&gt;org.apache.taglibs.request.QueryStringsTag&lt;/tagclass&gt;
41  * &lt;teiclass&gt;org.apache.taglibs.request.QueryStringsTEI&lt;/teiclass&gt;
42  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
43  * &lt;info&gt;Used to loop through all querystring parameters or get a single querystring 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 QueryStringsTag extends BodyTagSupport
60 {
61     private String JavaDoc name = null;
62     private HttpServletRequest req = null;
63     private StringTokenizer querystrings = null;
64     private StringTokenizer querystring = null;
65     private String JavaDoc parameter = null;
66     private String JavaDoc value = null;
67     private String JavaDoc query = null;
68
69     /**
70      * Gets the querystring parameters that came with the request or
71      * querystring parameter with <b>name</b>.
72      *
73      * @return SKIP_BODY if no querystring or querystring parameter with <b>name</b> is not found, EVAL_BODY_TAG if querystring parameter exists
74      */

75     public final int doStartTag() throws JspException
76     {
77     // Get the request
78
String JavaDoc qs =
79         ((HttpServletRequest)pageContext.getRequest()).getQueryString();
80     if( qs == null || qs.length() <= 0 )
81         return SKIP_BODY;
82     querystrings = new StringTokenizer(qs,"&");
83
84     
85     if( name != null ) {
86         String JavaDoc prefix = name + "=";
87         String JavaDoc tmp = null;
88         while( querystrings.hasMoreTokens() ) {
89         tmp = querystrings.nextToken();
90         if( tmp.startsWith(prefix) ) {
91             query = tmp;
92             querystring = new StringTokenizer(tmp,"=");
93             break;
94             }
95         }
96     } else {
97         if( !querystrings.hasMoreTokens() )
98         return SKIP_BODY;
99         query = querystrings.nextToken();
100         if( query == null )
101         return SKIP_BODY;
102         querystring = new StringTokenizer(query,"=");
103     }
104         if( querystring == null )
105         return SKIP_BODY;
106
107     if( querystring.hasMoreTokens() )
108         parameter = querystring.nextToken();
109         if( querystring.hasMoreTokens() )
110             value = querystring.nextToken();
111
112     pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE);
113     return EVAL_BODY_TAG;
114     }
115
116     /**
117      * Method called at end of each querystrings tag.
118      *
119      * @return EVAL_BODY_TAG if there is another querystring, or SKIP_BODY if there are no more querystrings or this is a named querystring
120      */

121     public final int doAfterBody() throws JspException
122     {
123     // See if this is the last or a named querystring
124
if( name != null || !querystrings.hasMoreTokens() )
125         return SKIP_BODY;
126     // There is another querystring, so loop again
127
query = querystrings.nextToken();
128         if( query == null )
129             return SKIP_BODY;
130     querystring = new StringTokenizer(query,"=");
131     if( querystring == null )
132         return SKIP_BODY;
133
134     parameter = value = null;
135         if( querystring.hasMoreTokens() )
136             parameter = querystring.nextToken();
137         if( querystring.hasMoreTokens() )
138             value = querystring.nextToken();
139     return EVAL_BODY_TAG;
140     }
141
142     /**
143      * Method called at end of Tag
144      * @return EVAL_PAGE
145      */

146     public final int doEndTag() throws JspException
147     {
148         pageContext.removeAttribute(id,PageContext.PAGE_SCOPE);
149     try
150     {
151         if(bodyContent != null)
152         bodyContent.writeOut(bodyContent.getEnclosingWriter());
153     } catch(java.io.IOException JavaDoc e)
154     {
155         throw new JspException("IO Error: " + e.getMessage());
156     }
157     return EVAL_PAGE;
158     }
159
160     /**
161      * Set the optional tag attribute <b>name</b>.
162      *
163      * @param String name of input parameter in querystring
164      */

165     public final void setName(String JavaDoc str)
166     {
167     name = str;
168     }
169
170     /**
171      * Returns the name of the querystring parameter.
172      * <p>
173      * &lt;jsp:getProperty name=<i>"id"</i> property="name"/&gt;
174      *
175      * @return String - querystring parameter name as a String
176      */

177     public final String JavaDoc getName()
178     {
179     if( parameter == null )
180         return "";
181     return parameter;
182     }
183
184     /**
185      * Returns the value of the querystring parameter.
186      * <p>
187      * &lt;jsp:getProperty name=<i>"id"</i> property="value"/&gt;
188      *
189      * @return String - value of the querystring parameter as a String
190      */

191     public final String JavaDoc getValue()
192     {
193     if( value == null )
194         return "";
195     return value;
196     }
197
198     /**
199      * Returns the entire querystring parameter/value.
200      * <p>
201      * &lt;jsp:getProperty name=<i>"id"</i> property="queryString"/&gt;
202      *
203      * @return String - entire querystring parameter/value as a String
204      */

205     public final String JavaDoc getQueryString()
206     {
207         if( query == null )
208             return "";
209         return query;
210     }
211
212 }
213
Popular Tags