KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > servlet > http > jsp > tagext > RequestParameterTag


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.servlet.http.jsp.tagext;
8
9 import javax.servlet.jsp.JspException JavaDoc;
10 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
11
12 import org.ejtools.util.ClassTools;
13
14 /**
15  * Tag that put a request parameter in the page context
16  * <p>
17  * The request parameter is put in the context with :<ul>
18  * <li>The <code>id</code> parameter : used as the identifier</li>
19  * <li>The <code>name</code> parameter : used to fetch the request parameter</li>
20  * <li>The <code>type</code> parameter : used to type the request parameter</li>
21  * <li>The <code>value</code> parameter : used as a default value if the request parameter is not found</li>
22  * </ul>
23  * </p>
24  *
25  * @author Laurent Etiemble
26  * @version $Revision: 1.4 $
27  * @jsp:tag name="requestParameter"
28  * body-content="empty"
29  */

30 public class RequestParameterTag extends TagSupport JavaDoc
31 {
32    /** Id property of the tag */
33    protected String JavaDoc id = null;
34    /** Name property of the tag */
35    protected String JavaDoc name = null;
36    /** Type property of the tag */
37    protected String JavaDoc type = null;
38    /** Value property of the tag */
39    protected String JavaDoc value = null;
40
41
42    /**
43     * Executed when the start tag is encountered
44     *
45     * @return An int value to resume or not the processing
46     * @exception JspException In case of problem
47     */

48    public int doStartTag()
49       throws JspException JavaDoc
50    {
51       String JavaDoc s = null;
52
53       s = pageContext.getRequest().getParameter(this.name);
54
55       if ((s == null) && (this.value == null))
56       {
57          throw new JspException JavaDoc("Request parameter " + this.name + " not found and value is not provided");
58       }
59
60       if (s == null)
61       {
62          s = this.value;
63       }
64       Object JavaDoc object = this.convert(s, this.type);
65
66       // Put the value in the pagecontext
67
pageContext.setAttribute(this.id, object);
68
69       // Continue processing this page
70
return (SKIP_BODY);
71    }
72
73
74    /**
75     * Gets the id value of this tag
76     *
77     * @return The id value
78     * @jsp:attribute name="id"
79     * required="true"
80     * rtexprvalue="true"
81     */

82    public String JavaDoc getId()
83    {
84       return this.id;
85    }
86
87
88    /**
89     * Gets the name value of this tag
90     *
91     * @return The name value
92     * @jsp:attribute name="name"
93     * required="true"
94     * rtexprvalue="true"
95     */

96    public String JavaDoc getName()
97    {
98       return this.name;
99    }
100
101
102    /**
103     * Gets the type value of this tag
104     *
105     * @return The type value
106     * @jsp:attribute name="type"
107     * required="false"
108     * rtexprvalue="true"
109     */

110    public String JavaDoc getType()
111    {
112       return this.type;
113    }
114
115
116    /**
117     * Gets the value value of this tag
118     *
119     * @return The value value
120     * @jsp:attribute name="value"
121     * required="false"
122     * rtexprvalue="true"
123     */

124    public String JavaDoc getValue()
125    {
126       return this.value;
127    }
128
129
130    /** Release all allocated resources. */
131    public void release()
132    {
133       super.release();
134       this.id = null;
135       this.name = null;
136       this.type = null;
137       this.value = null;
138    }
139
140
141    /**
142     * Sets the id value of this tag
143     *
144     * @param id The new id value
145     */

146    public void setId(String JavaDoc id)
147    {
148       this.id = id;
149    }
150
151
152    /**
153     * Sets the name value of this tag
154     *
155     * @param name The new name value
156     */

157    public void setName(String JavaDoc name)
158    {
159       this.name = name;
160    }
161
162
163    /**
164     * Sets the type value of this tag
165     *
166     * @param type The new type value
167     */

168    public void setType(String JavaDoc type)
169    {
170       this.type = type;
171    }
172
173
174    /**
175     * Sets the value value of this tag
176     *
177     * @param value The new value value
178     */

179    public void setValue(String JavaDoc value)
180    {
181       this.value = value;
182    }
183
184
185    /**
186     * Used to transform a string into a type object
187     *
188     * @param s The string representation of the object
189     * @param type The type of the object
190     * @return A type object
191     */

192    protected Object JavaDoc convert(String JavaDoc s, String JavaDoc type)
193    {
194       Class JavaDoc c = ClassTools.getClass(type);
195
196       if (c == Boolean JavaDoc.class)
197       {
198          return new Boolean JavaDoc(s);
199       }
200       if (c == Byte JavaDoc.class)
201       {
202          return new Byte JavaDoc(Byte.parseByte(s));
203       }
204       if (c == Double JavaDoc.class)
205       {
206          return new Double JavaDoc(Double.parseDouble(s));
207       }
208       if (c == Float JavaDoc.class)
209       {
210          return new Float JavaDoc(Float.parseFloat(s));
211       }
212       if (c == Integer JavaDoc.class)
213       {
214          return new Integer JavaDoc(Integer.parseInt(s));
215       }
216       if (c == Long JavaDoc.class)
217       {
218          return new Long JavaDoc(Long.parseLong(s));
219       }
220       return s;
221    }
222 }
223
224
Popular Tags