package com.util;
import java.math.BigDecimal;
import javax.servlet.http.HttpServletRequest;
/**
* request.getParameter封装
* @author Administrator
*
*/
public class ParamUtils {
private ParamUtils() {
}
/**
* 取得表单提交的数据,返回字符类型
* @param request
* @param s 表单域名称
* @param defaultString 默认值
* @return
* @throws Exception
*/
public static String getString(HttpServletRequest request, String s,
String defaultString) throws Exception {
String s1 = getString(request, s);
if (s1 == null) {
return defaultString;
} else {
return s1.trim();
}
}
/**
* 取得表单提交的数据,返回数值型
*
* @param request
* @param s
* @param defaultInt
* @return
*/
public static int getInt(HttpServletRequest request, String s,
int defaultInt) {
int j = 0;
try {
String temp = getString(request, s);
if (temp == null ) {
j = defaultInt;
}else if(temp.trim().equals("")){
j = defaultInt;
} else {
j = Integer.parseInt(temp);
}
} catch (NumberFormatException e) {
j = 0;
}
return j;
}
/**
* 取得表单提交的数据,返回数据封装类型
* @param request
* @param s http://www.javalearns.com/
* @param defaultInt
* @return
*/
public static Integer getInteger(HttpServletRequest request, String s,
int defaultInt) {
int v = ParamUtils.getInt(request, s, defaultInt);
if(v==defaultInt){
return null;
}else{
return new Integer(v);
}
}
/**
* 取得表单提交的数据,返回long类型
*
* @param request
* @param s
* @param defaultLong
* @return
*/
public static long getLong(HttpServletRequest request, String s,
long defaultLong) {
long l = 0;
try {
String temp = getString(request, s);
if (temp == null) {
l = defaultLong;
} else {
l = Long.parseLong(temp);
}
} catch (NumberFormatException e) {
l = 0;
}
return l;
}
/**
* 取得表单提交的数据,返回float类型
*
* @param request
* @param s
* @param defaultFloat
* @return
*/
public static float getFloat(HttpServletRequest request, String s,
float defaultFloat) {
float l = 0;
try {
String temp = getString(request, s);
if (temp == null) {
l = defaultFloat;
} else {
l = Float.parseFloat(temp);
}
} catch (NumberFormatException e) {
l = 0;
}
return l;
}
/**
* 取得表单提交的数据,返回double类型
* @param request
* @param s http://www.javalearns.com/
* @param defaultDouble
* @return
*/
public static double getDouble(HttpServletRequest request, String s,
double defaultDouble) {
double l = 0;
try {
String temp = getString(request, s);
if (temp == null) {
l = defaultDouble;
} else {
l = Double.parseDouble(temp);
}
} catch (NumberFormatException e) {
l = 0;
}
return l;
}
/**
* 取得表单提交的数据,返回Decimal类型
* @param request
* @param s
* @param defaultDouble
* @return
*/
public static BigDecimal getDecimal(HttpServletRequest request, String s,
double defaultDouble) {
BigDecimal l = null;
try {
String temp = getString(request, s);
if (temp == null) {
// l = defaultDouble;
l = null;
} else {
l = new BigDecimal(temp);
}
} catch (NumberFormatException e) {
// l = 0;
}
return l;
}
private static String getString(HttpServletRequest request, String s) {
String temp = null;
try {
temp = request.getParameter(s);
} catch (Exception exception) {
System.out.println(exception);
}
return temp;
}
}