KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > webapp > front > ParamUtils


1 /*
2  * NEMESIS-FORUM.
3  * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  *
7  * Copyright (C) 2001 Yasna.com. All rights reserved.
8  *
9  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
10  *
11  * NEMESIS-FORUM. is free software; you can redistribute it and/or
12  * modify it under the terms of the Apache Software License, Version 1.1,
13  * or (at your option) any later version.
14  *
15  * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
16  * application are parts of NEMESIS-FORUM and are distributed under
17  * same terms of licence.
18  *
19  *
20  * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
21  * and software developed by CoolServlets.com (http://www.coolservlets.com).
22  * and software developed by Yasna.com (http://www.yasna.com).
23  *
24  */

25
26 package org.nemesis.forum.webapp.front;
27
28 import javax.servlet.http.*;
29
30 /**
31  * This class assists skin writers in getting parameters.
32  */

33 public class ParamUtils {
34
35     /**
36      * Gets a parameter as a string.
37      * @param request The HttpServletRequest object, known as "request" in a
38      * JSP page.
39      * @param paramName The name of the parameter you want to get
40      * @return The value of the parameter or null if the parameter was not
41      * found or if the parameter is a zero-length string.
42      */

43         public static String JavaDoc getParameter( HttpServletRequest request, String JavaDoc paramName ) {
44                 return getParameter( request, paramName, false );
45         }
46
47     /**
48      * Gets a parameter as a string.
49      * @param request The HttpServletRequest object, known as "request" in a
50      * JSP page.
51      * @param paramName The name of the parameter you want to get
52      * @param emptyStringsOK Return the parameter values even if it is an empty string.
53      * @return The value of the parameter or null if the parameter was not
54      * found.
55      */

56         public static String JavaDoc getParameter( HttpServletRequest request, String JavaDoc paramName, boolean emptyStringsOK ) {
57                 String JavaDoc temp = request.getParameter(paramName);
58         if( temp != null ) {
59             if( temp.equals("") && !emptyStringsOK ) {
60                 return null;
61             }
62             else {
63                 return temp;
64             }
65         }
66         else {
67             return null;
68         }
69     }
70
71     /**
72      * Gets a parameter as a boolean.
73      * @param request The HttpServletRequest object, known as "request" in a
74      * JSP page.
75      * @param paramName The name of the parameter you want to get
76      * @return True if the value of the parameter was "true", false otherwise.
77      */

78         public static boolean getBooleanParameter( HttpServletRequest request, String JavaDoc paramName ) {
79                 String JavaDoc temp = request.getParameter(paramName);
80                 if( temp != null && temp.equals("true") ) {
81                         return true;
82                 } else {
83                         return false;
84                 }
85         }
86
87     /**
88      * Gets a parameter as a int.
89      * @param request The HttpServletRequest object, known as "request" in a
90      * JSP page.
91      * @param paramName The name of the parameter you want to get
92      * @return The int value of the parameter specified or the default value if
93      * the parameter is not found.
94      */

95         public static int getIntParameter( HttpServletRequest request, String JavaDoc paramName, int defaultNum ) {
96                 String JavaDoc temp = request.getParameter(paramName);
97                 if( temp != null && !temp.equals("") ) {
98             int num = defaultNum;
99             try {
100                 num = Integer.parseInt(temp);
101             }
102             catch( Exception JavaDoc ignored ) {}
103                         return num;
104                 } else {
105                         return defaultNum;
106                 }
107         }
108
109     /**
110      * Gets a checkbox parameter value as a boolean.
111      * @param request The HttpServletRequest object, known as "request" in a
112      * JSP page.
113      * @param paramName The name of the parameter you want to get
114      * @return True if the value of the checkbox is "on", false otherwise.
115      */

116     public static boolean getCheckboxParameter( HttpServletRequest request, String JavaDoc paramName ) {
117         String JavaDoc temp = request.getParameter(paramName);
118         if( temp != null && temp.equals("on") ) {
119             return true;
120         } else {
121             return false;
122         }
123     }
124
125     /**
126      * Gets a parameter as a string.
127      * @param request The HttpServletRequest object, known as "request" in a
128      * JSP page.
129      * @param attribName The name of the parameter you want to get
130      * @return The value of the parameter or null if the parameter was not
131      * found or if the parameter is a zero-length string.
132      */

133         public static String JavaDoc getAttribute( HttpServletRequest request, String JavaDoc attribName ) {
134                 return getAttribute( request, attribName, false );
135         }
136
137     /**
138      * Gets a parameter as a string.
139      * @param request The HttpServletRequest object, known as "request" in a
140      * JSP page.
141      * @param attribName The name of the parameter you want to get
142      * @param emptyStringsOK Return the parameter values even if it is an empty string.
143      * @return The value of the parameter or null if the parameter was not
144      * found.
145      */

146         public static String JavaDoc getAttribute( HttpServletRequest request, String JavaDoc attribName, boolean emptyStringsOK ) {
147                 String JavaDoc temp = (String JavaDoc)request.getAttribute(attribName);
148         if( temp != null ) {
149             if( temp.equals("") && !emptyStringsOK ) {
150                 return null;
151             }
152             else {
153                 return temp;
154             }
155         }
156         else {
157             return null;
158         }
159     }
160
161     /**
162      * Gets an attribute as a boolean.
163      * @param request The HttpServletRequest object, known as "request" in a
164      * JSP page.
165      * @param attribName The name of the attribute you want to get
166      * @return True if the value of the attribute is "true", false otherwise.
167      */

168         public static boolean getBooleanAttribute( HttpServletRequest request, String JavaDoc attribName ) {
169                 String JavaDoc temp = (String JavaDoc)request.getAttribute(attribName);
170                 if( temp != null && temp.equals("true") ) {
171                         return true;
172                 } else {
173                         return false;
174                 }
175         }
176
177     /**
178      * Gets an attribute as a int.
179      * @param request The HttpServletRequest object, known as "request" in a
180      * JSP page.
181      * @param attribName The name of the attribute you want to get
182      * @return The int value of the attribute or the default value if the attribute is not
183      * found or is a zero length string.
184      */

185         public static int getIntAttribute( HttpServletRequest request, String JavaDoc attribName, int defaultNum ) {
186                 String JavaDoc temp = (String JavaDoc)request.getAttribute(attribName);
187                 if( temp != null && !temp.equals("") ) {
188             int num = defaultNum;
189             try {
190                 num = Integer.parseInt(temp);
191             }
192             catch( Exception JavaDoc ignored ) {}
193                         return num;
194                 } else {
195                         return defaultNum;
196                 }
197         }
198
199 }
200
Popular Tags