728x90
반응형
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | package xxx.xxx.common.web; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import javax.servlet.http.HttpServletRequest; import net.bogo.common.util.StringUtil; public class ParameterUtils { public static boolean isEmpty(HttpServletRequest request, String param) { return ((request.getParameter(param) == null) || (request.getParameter(param).equals(""))); } public static String getStringParameter(HttpServletRequest request, String param, String def) { if (isEmpty(request, param)) { return def; } return request.getParameter(param); } public static int getIntegerParameter(HttpServletRequest request, String param, int def) { if (isEmpty(request, param)) { return def; } String str = request.getParameter(param); return StringUtil.toInteger(str, def); } public static String setParameterValue(HttpServletRequest request, String paramName, String paramValue) throws UnsupportedEncodingException { String queryString = request.getQueryString(); StringBuilder newQueryString = new StringBuilder(); String[] parameter = queryString.split("\\&"); for (String s : parameter) { String[] p = s.split("\\="); if (p.length != 2) continue; if (p[0].equals(paramName)) { p[1] = paramValue; } p[1] = URLEncoder.encode(p[1], "UTF-8"); newQueryString.append(p[0] + "=" + p[1] + "&"); } return newQueryString.toString(); } public static Map<String, Object> getRequestParameterMap(HttpServletRequest req) { Map<String, Object> reqParamMaps = new HashMap<String, Object>(); Map<String, Object> reqMap = new HashMap<String, Object>(); reqParamMaps.putAll(req.getParameterMap()); Set<?> keySet = reqParamMaps.keySet(); for (Object key : keySet) { Object obj = reqParamMaps.get(key); if (obj.getClass().isArray() || Array.getLength(obj) == 1) { reqMap.put(key.toString(), Array.get(obj, 0)); } else { reqMap.put(key.toString(), obj); } } return reqMap; } } Colored by Color Scripter cs | cs |
728x90
반응형