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

📄 requestutility.java

📁 采用JAVA开发
💻 JAVA
字号:
package com.gctech.misc.util;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletRequest;
/**
 * <p>Title:</p>
 * <p>Description:处理输入值的通用方法</p>
 * <p>Copyright: Copyright (c) 2004-1-8</p>
 * <p>Company: 中软远东国际</p>
 *
 * @version 1.0
 * @author Eric.Li
 *
 */
public class RequestUtility {
    /**
     * 默认返回空格
     * 
     * @param request
     * @param name 参数名
     * @return 
     */
    public static String getParam(ServletRequest request, String name) {
        return getParam(request, name, "", false);
    }
    /**
     * 如果输入为空返回指定默认值
     * 
     * @param request 
     * @param name 参数名
     * @param defaultValue 默认值
     * @param filter 是否经过转码
     * @return 
     */
    public static String getParam(ServletRequest request, String name, String defaultValue, boolean filter) {
        String value = request.getParameter(name);
        if (value == null || value.equals("")) {
            return defaultValue;
        }
        else {
            if (filter)
                return UnicodeToChinese(value);
            else
                return value;
        }
    }
    /**
     * 整型默认返回0
     * 
     * @param request 
     * @param name 参数名
     * @return 
     */
    public static Integer getIntegerParam(ServletRequest request, String name) {
        String value = getParam(request, name);
        Integer result = new Integer(0);
        try {
            result = new Integer(value);
        }
        catch (Exception e) {
            //e.printStackTrace();
        }
        return result;
    }
    /**
     * 整型默认默认值
     * 
     * @param request 
     * @param name 参数名
     * @return 
     */
    public static Integer getIntegerParam(ServletRequest request, String name, Integer defaultValue) {
        String value = getParam(request, name);
        Integer result = defaultValue;
        try {
            result = new Integer(value);
        }
        catch (Exception e) {
            //e.printStackTrace();
        }
        return result;
    }
    /**
     * 处理输入中的复选框
     * 
     * @param request
     * @param name 参数名
     * @return
     */
    public static Integer[] getIntegerParamValues(ServletRequest request, String name) {
        String[] values = request.getParameterValues(name);
        if (values != null) {
            Integer[] results = new Integer[values.length];
            for (int i = 0; i < values.length; i++) {
                Integer result = new Integer(0);
                try {
                    result = new Integer(values[i]);
                }
                catch (Exception e) {
                    //e.printStackTrace();
                }
                results[i] = result;
            }
            return results;
        }
        else {
            return new Integer[0];
        }
    }
    /**
     * 转码
     * 
     * @param s
     * @return
     */
    public static String ChineseToUnicode(String s) {
        try {
            if (s == null || s.equals(""))
                return "";
            String newstring = null;
            newstring = new String(s.getBytes("gb2312"), "ISO8859_1");
            return newstring;
        }
        catch (UnsupportedEncodingException e) {
            return s;
        }
    }
    public static String UnicodeToChinese(String s) {
        try {
            if (s == null || s.equals(""))
                return "";
            String newstring = null;
            newstring = new String(s.getBytes("ISO8859_1"), "gb2312");
            return newstring;
        }
        catch (UnsupportedEncodingException e) {
            return s;
        }
    }
    
    //protected void forward(HttpServletRequest request, HttpServletResponse response, String strTarget) throws IOException, ServletException {
    //	getServletContext().getRequestDispatcher(strTarget).forward(request, response);
    //}
    

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -