分享一个参数转换工具类
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
* <p>Title:Request的参数转换</p>
* <li>从Request取得的各种变量类型的转换</li>
* <br><b>CopyRight: yyhweb[由由华网]</b>
* @author stephen
* @version YHBBS-2.0
*/
public class RequestUtils {
/**
* @param request request
*/
public static void setAttribute(HttpServletRequest request, String paramName, String paramValue) {
request.setAttribute(paramName, paramValue);
}
/**
* @param request request
*/
public static void removeAttribute(HttpServletRequest request, String paramName) {
request.removeAttribute(paramName);
}
/**
* @param request request
* @throws UnsupportedEncodingException
*/
public static void setCharacterEncoding(HttpServletRequest request) throws UnsupportedEncodingException {
request.setCharacterEncoding("UTF-8");
}
/**
* @param request
* @param paramName 参数名称
* @return 从request获取参数对应字符串值
*/
public static String getString(HttpServletRequest request, String paramName) {
String value = request.getParameter(paramName);
return value != null ? value : "";
}
/**
* @param request
* @param paramName 参数名称
* @return 从request获取参数对应字符串值
*/
public static String getQueryString(HttpServletRequest request, String paramName) {
String value = request.getQueryString();
if(null == value)
return "";
return value.substring(paramName.length()+1, value.length());
}
/**
* @param request
* @param paramName 参数名称
* @return 从request获取参数对应字符串值
*/
public static String getAttribute(HttpServletRequest request, String paramName) {
String value = (String) request.getAttribute(paramName);
return value != null ? value : "";
}
/**
* @param request
* @param paramName 参数名称
* @return 从request获取属性对应字符串值
*/
public static String getStrAtt(HttpServletRequest request, String paramName){
String value = (String) request.getAttribute(paramName);
return value != null ? value : "";
}
/**
* @param request
* @param paramName 参数名称
* @return 从request获取参数对应数组值
*/
public static String[] getArray(HttpServletRequest request, String paramName) {
return request.getParameterValues(paramName);
}
/**
* @param request
* @param paramName 参数名称
* @return 从request获取参数对应Byte值
*/
public static byte getByte(HttpServletRequest request, String paramName) {
String value = request.getParameter(paramName);
if (value == null || value.length() == 0)
return 0;
else
return Byte.parseByte(value);
}
/**
* @param request
* @param paramName 参数名称
* @return 从request获取参数对应整型值
*/
public static int getInt(HttpServletRequest request, String paramName) {
String value = request.getParameter(paramName);
if (value == null || value.length() == 0)
return 0;
else{
try{
return Integer.parseInt(value);
}catch(Exception e){
e.printStackTrace();
return 0;
}
}
}
/**
* @param request
* @param paramName 参数名称
* @return 从request获取参数对应整型值
*/
public static int getIntAtt(HttpServletRequest request, String paramName) {
String value = request.getAttribute(paramName).toString();
if (value == null || value.length() == 0)
return 0;
else{
try{
return Integer.parseInt(value);
}catch(Exception e){
e.printStackTrace();
return 0;
}
}
}
/**
* @param request
* @param paramName 参数名称
* @return 从request获取参数对应长整型值
*/
public static long getLong(HttpServletRequest request, String paramName) {
String value = request.getParameter(paramName);
if (value == null || value.length() == 0)
return 0L;
else{
try{
return Long.parseLong(value);
}catch(Exception e){
e.printStackTrace();
return 0L;
}
}
}
/**
* @param request
* @param paramName 参数名称
* @return 从request获取参数对应短整型值
*/
public static short getShort(HttpServletRequest request, String paramName) {
String value = request.getParameter(paramName);
if (value == null || value.length() == 0)
return 0;
else
return Short.parseShort(value);
}
/**
* @param request
* @param paramName 参数名称
* @return 从request获取参数对应布尔值
*/
public static boolean getBoolean(HttpServletRequest request,
String paramName) {
String value = request.getParameter(paramName);
if (value == null || value.length() == 0)
return false;
else
return Boolean.valueOf(value).booleanValue();
}
/**
*
* @param request
* @param paramName
* @return 从前端提交过来的JSON格式的字符串获取参数,并且返回一个JsonObject对象
*/
public static JsonObject getJsonObject(HttpServletRequest request, String paramName){
String value = (String) request.getAttribute(paramName);
JsonObject jsonObj = new JsonParser().parse(value).getAsJsonObject();
return jsonObj;
}
}