⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paramutils.java

📁 计算机技术的快速发展
💻 JAVA
字号:
package com.suninformation.tools;

import javax.servlet.http.*;

/**
 * 系统参数读取功能扩展类
 *
 * <p>Title: SunInformation's toolkit system</p>
 *
 * <p>Description: 系统常用工具包</p>
 *
 * <p>Copyright: Copyright (c) 2005-03-17</p>
 *
 * <p>Company: 梦想软件开发工作室</p>
 *
 * @author 刘镇
 * @version 1.0
 */
public class ParamUtils {

    /**
     * 获取一个字符串型变量。
     * @param request 一个HttpServletRequest对象, 常用JSP的"request"
     * @param name 你想获取的变量的名称。
     * @return 返回变量的值,如果变量没有找到或者变量是一个0长度的则返回空。
     */
    public static String getParameter(HttpServletRequest request, String name) {
        return getParameter(request, name, false);
    }

    /**
     * 获取一个字符串型变量。
     * @param request 一个HttpServletRequest对象, 常用JSP的"request"
     * @param name 你想获取的变量的名称。
     * @param emptyStringsOK 返回变量的值,即使它是一个空值。
     * @return 返回变量的值,如果变量没有找到或者变量是一个0长度的则返回空。
     */
    public static String getParameter(HttpServletRequest request,
                                      String name, boolean emptyStringsOK) {
        String temp = request.getParameter(name);
        if (temp != null) {
            if (temp.equals("") && !emptyStringsOK) {
                return null;
            } else {
                return temp;
            }
        } else {
            return null;
        }
    }

    /**
     * 获取一个布尔型变量。
     * @param request The HttpServletRequest object, known as "request" in a
     * JSP page.
     * @param name The name of the parameter you want to get
     * @return True if the value of the parameter was "true", false otherwise.
     */
    public static boolean getBooleanParameter(HttpServletRequest request,
                                              String name) {
        return getBooleanParameter(request, name, false);
    }

    /**
     * 获取一个布尔型变量。
     *
     * @param request The HttpServletRequest object, known as "request" in a JSP
     *   page.
     * @param name The name of the parameter you want to get
     * @param defaultVal boolean
     * @return True if the value of the parameter was "true", false otherwise.
     */
    public static boolean getBooleanParameter(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;
        }
    }

    /**
     * 获取一个整型变量。
     *
     * @param request The HttpServletRequest object, known as "request" in a JSP
     *   page.
     * @param name The name of the parameter you want to get
     * @param defaultNum int
     * @return The int value of the parameter specified or the default value if
     *   the parameter is not found.
     */
    public static int 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);
            } catch (Exception ignored) {}
            return num;
        } else {
            return defaultNum;
        }
    }

    /**
     * 获取一个整型变量数组。
     *
     * @param request The HttpServletRequest object, known as "request" in a JSP
     *   page.
     * @param name The name of the parameter you want to get
     * @param defaultNum The default value of a parameter, if the parameter can't
     *   be converted into an int.
     * @return int[]
     */
    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;
    }

    /**
     * 获取一个双精度型变量。
     *
     * @param request The HttpServletRequest object, known as "request" in a JSP
     *   page.
     * @param name The name of the parameter you want to get
     * @param defaultNum double
     * @return The double value of the parameter specified or the default value
     *   if the parameter is not found.
     */
    public static double 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);
            } catch (Exception ignored) {}
            return num;
        } else {
            return defaultNum;
        }
    }

    /**
     * 获取一个长整型变量。
     *
     * @param request The HttpServletRequest object, known as "request" in a JSP
     *   page.
     * @param name The name of the parameter you want to get
     * @param defaultNum long
     * @return The long value of the parameter specified or the default value if
     *   the parameter is not found.
     */
    public static long 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);
            } catch (Exception ignored) {}
            return num;
        } else {
            return defaultNum;
        }
    }

    /**
     * 获取一个长整型变量数组。
     *
     * @param request The HttpServletRequest object, known as "request" in a JSP
     *   page.
     * @param name The name of the parameter you want to get
     * @param defaultNum The default value of a parameter, if the parameter can't
     *   be converted into a long.
     * @return long[]
     */
    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;
    }

    /**
     * 获取一个字符串型属性值。
     * @param request The HttpServletRequest object, known as "request" in a
     *      JSP page.
     * @param name The name of the parameter you want to get
     * @return The value of the parameter or null if the parameter was not
     *      found or if the parameter is a zero-length string.
     */
    public static String getAttribute(HttpServletRequest request, String name) {
        return getAttribute(request, name, false);
    }

    /**
     * 获取一个字符串型属性值。
     * @param request The HttpServletRequest object, known as "request" in a
     *      JSP page.
     * @param name The name of the parameter you want to get
     * @param emptyStringsOK Return the parameter values even if it is an empty string.
     * @return The value of the parameter or null if the parameter was not
     *      found.
     */
    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 {
                return temp;
            }
        } else {
            return null;
        }
    }

    /**
     * 获取一个布尔型属性值。
     * @param request The HttpServletRequest object, known as "request" in a
     *      JSP page.
     * @param name The name of the attribute you want to get
     * @return True if the value of the attribute is "true", false otherwise.
     */
    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;
        }
    }

    /**
     * 获取一个整型属性值。
     *
     * @param request The HttpServletRequest object, known as "request" in a JSP
     *   page.
     * @param name The name of the attribute you want to get
     * @param defaultNum int
     * @return The int value of the attribute or the default value if the
     *   attribute is not found or is a zero length string.
     */
    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;
        }
    }

    /**
     * 获取一个长整型属性值。
     *
     * @param request The HttpServletRequest object, known as "request" in a JSP
     *   page.
     * @param name The name of the attribute you want to get
     * @param defaultNum long
     * @return The long value of the attribute or the default value if the
     *   attribute is not found or is a zero length string.
     */
    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 + -