`

ParamUtils参数接受工具类的代码及介绍

    博客分类:
  • jsp
阅读更多


ParamUtils这个类,觉得以后做项目时可以把做成一个单独的组件使用,解决了request.getParameter()时只能取出String的问题。

/**
* $RCSfile: ParamUtils.java,v $
* $Revision: 1.6 $
* $Date: 2001/07/31 05:38:18 $
*
* Copyright (C) 1999-2001 CoolServlets, Inc. All rights reserved.
*
* This software is the proprietary information of CoolServlets, Inc.
* Use is subject to license terms.
*/

package com.jivesoftware.forum.util;

import javax.servlet.http.*;

/**
* This class assists skin writers in getting parameters.
*/
public class ParamUtils {

    /**
     * Gets a parameter as a string.
     * @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 getParameter(HttpServletRequest request, String name) {
        return getParameter(request, name, false);
    }

    /**
     * Gets a parameter as a string.
     * @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 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;
        }
    }

    /**
     * Gets a parameter as a boolean.
     * @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);
    }

    /**
     * Gets a parameter as a boolean.
     * @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, 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;
        }
    }

    /**
     * Gets a parameter as an int.
     * @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 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;
        }
    }

    /**
     * Gets a list of int parameters.
     * @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.
     */
    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;
    }

    /**
     * Gets a parameter as a double.
     * @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 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;
        }
    }

    /**
     * Gets a parameter as a long.
     * @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 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;
        }
    }

    /**
     * Gets a list of long parameters.
     * @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.
     */
    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;
    }

    /**
     * Gets a parameter as a string.
     * @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);
    }

    /**
     * Gets a parameter as a string.
     * @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;
        }
    }

    /**
     * Gets an attribute as a boolean.
     * @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;
        }
    }

    /**
     * Gets an attribute as a int.
     * @param request The HttpServletRequest object, known as "request" in a
     *      JSP page.
     * @param name The name of the attribute you want to get
     * @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;
        }
    }

    /**
     * Gets an attribute as a long.
     * @param request The HttpServletRequest object, known as "request" in a
     *      JSP page.
     * @param name The name of the attribute you want to get
     * @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;
        }
     }
}
分享到:
评论

相关推荐

    JSP参数接收工具类.rar

    在这个"JSP参数接收工具类.rar"压缩包中,包含了两个重要的文件:"JSP参数接收工具类(ParamUtils.txt)"和"类加载工具.txt"。这些文件很可能是为了帮助开发者更方便地管理和处理JSP页面传入的请求参数,以及理解类...

    Smart2005中间件平台.doc

    总之,Smart2005中间件平台通过ParamUtils类提供了一套全面的工具,用于处理Web应用中的参数传递和类型转换。这些功能对于构建高效、可靠的互联网服务至关重要,它们帮助开发者快速响应用户请求,同时保持代码的整洁...

    一次获得界面所有实体类的数据

    `ParamUtils`类根据setter方法的参数类型(如`int.class`、`Date.class`等),使用适当的转换方法(如`Integer.parseInt`、`SimpleDateFormat.parse`等)将字符串参数转换为正确的类型。 #### 总结 通过`...

    石大在线财务管理系统(含源码)_shidacaiwu(毕设 + 课设).zip

    4. **参数处理**:`ParamUtils.class`可能是一个工具类,用于处理HTTP请求中的参数,这在处理用户输入和数据验证时非常关键,确保了数据安全。 5. **图片资源**:`close.gif`和`reset.gif`是图形资源,常用于网页中...

    Jive源代码情景分析-forum.jsp

    这部分代码较为简单,主要负责获取请求参数并加载指定的论坛信息。具体代码如下: ```jsp long forumID = ParamUtils.getLongParameter(request, "forum", -1L); int start = ParamUtils.getIntParameter(request,...

    JAVA Web程序设计-3期(KC006) 04项目案例_《企业门户网站》设计文档.doc

    - 开发及运行环境:定义开发环境,如Java开发工具(JDK)、集成开发环境(IDE,如Eclipse或IntelliJ IDEA),以及运行环境,如服务器软件(Tomcat)、数据库管理系统(MySQL)等。 - 数据库设计:设计数据库表结构,包括...

    Java Web应用开发 《Java Web应用开发》_项目库_项目案例_《企业门户网站》设计文档.doc

    - **开发及运行环境**:选择合适的开发工具(如IDE、服务器)、编程语言(如Java)、框架(如Spring、Struts、Hibernate)和数据库(如MySQL、Oracle)。 - **数据库设计**:包括数据表设计、关系设计,确保数据的...

    JAVA通过Session和Cookie实现网站自动登录的技术

    本文将详细介绍如何使用 Session 和 Cookie 实现网站自动登录的技术。 一、什么是 Session 和 Cookie? Session 和 Cookie 是两种常用的Web开发技术。Session 是一种服务器端的存储机制,用于存储用户的会话信息。...

    校内网络_用户管理模块 管理员设置向导

    校园网数据库,sql数据库的应用,有存储...String pagenumber = ParamUtils.getParameter(request,"page"); //out.println(UserManager.getUserProxy().showUserListByPage("","username",pagenumber)); %&gt; &lt;/html&gt;

    cookie自动登录认证

    if (ParamUtils.getBooleanParameter(request, "savePassword")) { // 设置密码相关的Cookie cookie = new Cookie("SESSION_LOGIN_PASSWORD", MD5.encode(u.getPassword())); cookie.setPath("/"); cookie....

Global site tag (gtag.js) - Google Analytics