随笔-200  评论-148  文章-15  trackbacks-0
				
						package
				 com.chinajavaworld.util;
 
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
 
publicclass ParamUtils
{
 
public ParamUtils()
{
}
 
publicstatic String getParameter(HttpServletRequest request, String name)
{
return getParameter(request, name, false);
}
 
publicstatic String getParameter(HttpServletRequest request, String name, String defaultValue)
{
return getParameter(request, name, defaultValue, false);
}
 
publicstatic String getParameter(HttpServletRequest request, String name, boolean emptyStringsOK)
{
return getParameter(request, name, null, emptyStringsOK);
}
 
publicstatic String getParameter(HttpServletRequest request, String name, String defaultValue, boolean emptyStringsOK)
{
String temp = request.getParameter(name);
if(temp != null)
{
if(temp.equals("") && !emptyStringsOK)
return defaultValue;
else
return temp;
}else
{
return defaultValue;
}
}
 
publicstatic String[] getParameters(HttpServletRequest request, String name)
{
if(name == null)
returnnew String[0];
String paramValues[] = request.getParameterValues(name);
if(paramValues == null || paramValues.length == 0)
returnnew String[0];
List values = new ArrayList(paramValues.length);
for(int i = 0; i < paramValues.length; i++)
if(paramValues[i] != null && !"".equals(paramValues[i]))
values.add(paramValues[i]);
 
return (String[])values.toArray(new String[0]);
}
 
publicstaticboolean getBooleanParameter(HttpServletRequest request, String name)
{
return getBooleanParameter(request, name, false);
}
 
publicstaticboolean getBooleanParameter(HttpServletRequest request, String name, boolean defaultVal)
{
String temp = request.getParameter(name);
if("true".equals(temp) || "on".equals(temp))
returntrue;
if("false".equals(temp) || "off".equals(temp))
returnfalse;
else
return defaultVal;
}
 
publicstaticint getIntParameter(HttpServletRequest request, String name, int defaultNum)
{
String temp = request.getParameter(name);
if(temp != null && !temp.equals(""))
{
int num = defaultNum;
try
{
num = Integer.parseInt(temp.trim());
}
catch(Exception ignored) {}
return num;
}else
{
return defaultNum;
}
}
 
publicstaticint[] getIntParameters(HttpServletRequest request, String name, int defaultNum)
{
String paramValues[] = request.getParameterValues(name);
if(paramValues == null || paramValues.length == 0)
returnnewint[0];
int values[] = newint[paramValues.length];
for(int i = 0; i < paramValues.length; i++)
try
{
values[i] = Integer.parseInt(paramValues[i].trim());
}
catch(Exception e)
{
values[i] = defaultNum;
}
 
return values;
}
 
publicstaticdouble getDoubleParameter(HttpServletRequest request, String name, double defaultNum)
{
String temp = request.getParameter(name);
if(temp != null && !temp.equals(""))
{
double num = defaultNum;
try
{
num = Double.parseDouble(temp.trim());
}
catch(Exception ignored) {}
return num;
}else
{
return defaultNum;
}
}
 
publicstaticlong getLongParameter(HttpServletRequest request, String name, long defaultNum)
{
String temp = request.getParameter(name);
if(temp != null && !temp.equals(""))
{
long num = defaultNum;
try
{
num = Long.parseLong(temp.trim());
}
catch(Exception ignored) {}
return num;
}else
{
return defaultNum;
}
}
 
publicstaticlong[] getLongParameters(HttpServletRequest request, String name, long defaultNum)
{
String paramValues[] = request.getParameterValues(name);
if(paramValues == null || paramValues.length == 0)
returnnewlong[0];
long values[] = newlong[paramValues.length];
for(int i = 0; i < paramValues.length; i++)
try
{
values[i] = Long.parseLong(paramValues[i].trim());
}
catch(Exception e)
{
values[i] = defaultNum;
}
 
return values;
}
 
publicstatic String getAttribute(HttpServletRequest request, String name)
{
return getAttribute(request, name, false);
}
 
publicstatic String getAttribute(HttpServletRequest request, String name, boolean emptyStringsOK)
{
String temp = (String)request.getAttribute(name);
if(temp != null)
{
if(temp.equals("") && !emptyStringsOK)
returnnull;
else
return temp;
}else
{
returnnull;
}
}
 
publicstaticboolean getBooleanAttribute(HttpServletRequest request, String name)
{
String temp = (String)request.getAttribute(name);
return temp != null && temp.equals("true");
}
 
publicstaticint getIntAttribute(HttpServletRequest request, String name, int defaultNum)
{
String temp = (String)request.getAttribute(name);
if(temp != null && !temp.equals(""))
{
int num = defaultNum;
try
{
num = Integer.parseInt(temp.trim());
}
catch(Exception ignored) {}
return num;
}else
{
return defaultNum;
}
}
 
publicstaticlong getLongAttribute(HttpServletRequest request, String name, long defaultNum)
{
String temp = (String)request.getAttribute(name);
if(temp != null && !temp.equals(""))
{
long num = defaultNum;
try
{
num = Long.parseLong(temp.trim());
}
catch(Exception ignored) {}
return num;
}else
{
return defaultNum;
}
}
}
posted on 2006-10-11 17:13 无声 阅读(1726) 评论(0)  编辑  收藏 所属分类: java共享代码

只有注册用户登录后才能发表评论。


网站导航: