📄 paramutil.java
字号:
package cn.js.fan.util;import javax.servlet.http.*;import cn.js.fan.web.Global;public class ParamUtil { public static String get(HttpServletRequest request, String name) { return get(request, name, true); } public static String get(HttpServletRequest request, String name, boolean emptyStringsOK) { String temp = request.getParameter(name); if (temp != null) { temp = temp.trim(); if (temp.equals("") && !emptyStringsOK) { return null; } else { if (Global.requestSupportCN) { return temp; } else { return StrUtil.UnicodeToUTF8(temp); } } } else { if (emptyStringsOK) return ""; else return null; } } public static String[] getParameters(HttpServletRequest request, String name) { String[] ary = request.getParameterValues(name); if (ary==null) return null; int arylen = ary.length; if (!Global.requestSupportCN) { for (int i = 0; i < arylen; i++) ary[i] = StrUtil.UnicodeToUTF8(ary[i]); } return ary; } public static boolean getBooleanParameter(HttpServletRequest request, String name) { return getBoolean(request, name, false); } public static boolean getBoolean(HttpServletRequest request, String name, boolean defaultVal) { String temp = request.getParameter(name); if ("true".equals(temp) || "on".equals(temp)) { return true; } else if ("false".equals(temp) || "off".equals(temp)) { return false; } else { return defaultVal; } } public static int getInt(HttpServletRequest request, String name) throws ErrMsgException { return getInt(request, name, true); } public static int getInt(HttpServletRequest request, String name, int defaultValue) { int num = defaultValue; String temp = request.getParameter(name); try { num = Integer.parseInt(temp); } catch (Exception e) { } return num; } public static int getInt(HttpServletRequest request, String name, boolean isThrow) throws ErrMsgException { String temp = request.getParameter(name); int num = 0; try { num = Integer.parseInt(temp); } catch (Exception e) { if (isThrow) throw new ErrMsgException(name + "不是整数!"); } return num; } public static int[] getIntParameters(HttpServletRequest request, String name, int defaultNum) { String[] paramValues = request.getParameterValues(name); if (paramValues == null) { return null; } if (paramValues.length < 1) { return new int[0]; } int[] values = new int[paramValues.length]; for (int i=0; i<paramValues.length; i++) { try { values[i] = Integer.parseInt(paramValues[i]); } catch (Exception e) { values[i] = defaultNum; } } return values; } public static double getDouble(HttpServletRequest request, String name, double defaultNum) { String temp = request.getParameter(name); if(temp != null && !temp.equals("")) { double num = defaultNum; try { num = Double.parseDouble(temp); } catch (Exception ignored) {} return num; } else { return defaultNum; } } public static double getDouble(HttpServletRequest request, String name) throws ErrMsgException { String temp = request.getParameter(name); if(temp != null && !temp.equals("")) { double num = 0.0; try { num = Double.parseDouble(temp); } catch (Exception ignored) { throw new ErrMsgException(name + " is not of double type !"); } return num; } else { throw new ErrMsgException(name + " is null or empty!"); } } public static long getLong(HttpServletRequest request, String name) throws ErrMsgException { long num = -1; String temp = request.getParameter(name); try { num = Long.parseLong(temp); } catch (Exception e) { throw new ErrMsgException("Name=" + name + " value=" + temp + " is not a long value!"); } return num; } public static long getLong(HttpServletRequest request, String name, long defaultNum) { String temp = request.getParameter(name); if (temp != null && !temp.equals("")) { long num = defaultNum; try { num = Long.parseLong(temp); } catch (Exception ignored) {} return num; } else { return defaultNum; } } public static long[] getLongParameters(HttpServletRequest request, String name, long defaultNum) { String[] paramValues = request.getParameterValues(name); if (paramValues == null) { return null; } if (paramValues.length < 1) { return new long[0]; } long[] values = new long[paramValues.length]; for (int i=0; i<paramValues.length; i++) { try { values[i] = Long.parseLong(paramValues[i]); } catch (Exception e) { values[i] = defaultNum; } } return values; } public static String getAttribute(HttpServletRequest request, String name) { return getAttribute (request, name, false); } public static String getAttribute(HttpServletRequest request, String name, boolean emptyStringsOK) { String temp = (String)request.getAttribute(name); if (temp != null) { if (temp.equals("") && !emptyStringsOK) { return null; } else { if (Global.requestSupportCN) { return temp; } else return StrUtil.UnicodeToUTF8(temp); } } else { return null; } } public static boolean getBooleanAttribute(HttpServletRequest request, String name) { String temp = (String)request.getAttribute(name); if (temp != null && temp.equals("true")) { return true; } else { return false; } } public static int 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); } catch (Exception ignored) {} return num; } else { return defaultNum; } } public static long 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); } catch (Exception ignored) {} return num; } else { return defaultNum; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -