Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 271|回复: 0

[Java基础知识]request参数接收解析工具

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-1 21:07:33 | 显示全部楼层 |阅读模式
    1.    
    2. import java.util.ArrayList;
    3. import java.util.List;
    4. import javax.servlet.http.HttpServletRequest;

    5. public class ParamUtils
    6. {

    7.     public ParamUtils()
    8.     {
    9.     }

    10.     public static String getParameter(HttpServletRequest request, String name)
    11.     {
    12.         return getParameter(request, name, false);
    13.     }

    14.     public static String getParameter(HttpServletRequest request, String name, String defaultValue)
    15.     {
    16.         return getParameter(request, name, defaultValue, false);
    17.     }

    18.     public static String getParameter(HttpServletRequest request, String name, boolean emptyStringsOK)
    19.     {
    20.         return getParameter(request, name, null, emptyStringsOK);
    21.     }

    22.     public static String getParameter(HttpServletRequest request, String name, String defaultValue, boolean emptyStringsOK)
    23.     {
    24.         String temp = request.getParameter(name);
    25.         if(temp != null)
    26.         {
    27.             if(temp.equals("") && !emptyStringsOK)
    28.                 return defaultValue;
    29.             else
    30.                 return temp;
    31.         } else
    32.         {
    33.             return defaultValue;
    34.         }
    35.     }

    36.     public static String[] getParameters(HttpServletRequest request, String name)
    37.     {
    38.         if(name == null)
    39.             return new String[0];
    40.         String paramValues[] = request.getParameterValues(name);
    41.         if(paramValues == null || paramValues.length == 0)
    42.             return new String[0];
    43.         List values = new ArrayList(paramValues.length);
    44.         for(int i = 0; i < paramValues.length; i++)
    45.             if(paramValues[i] != null && !"".equals(paramValues[i]))
    46.                 values.add(paramValues[i]);

    47.         return (String[])values.toArray(new String[0]);
    48.     }

    49.     public static boolean getBooleanParameter(HttpServletRequest request, String name)
    50.     {
    51.         return getBooleanParameter(request, name, false);
    52.     }

    53.     public static boolean getBooleanParameter(HttpServletRequest request, String name, boolean defaultVal)
    54.     {
    55.         String temp = request.getParameter(name);
    56.         if("true".equals(temp) || "on".equals(temp))
    57.             return true;
    58.         if("false".equals(temp) || "off".equals(temp))
    59.             return false;
    60.         else
    61.             return defaultVal;
    62.     }

    63.     public static int getIntParameter(HttpServletRequest request, String name, int defaultNum)
    64.     {
    65.         String temp = request.getParameter(name);
    66.         if(temp != null && !temp.equals(""))
    67.         {
    68.             int num = defaultNum;
    69.             try
    70.             {
    71.                 num = Integer.parseInt(temp.trim());
    72.             }
    73.             catch(Exception ignored) { }
    74.             return num;
    75.         } else
    76.         {
    77.             return defaultNum;
    78.         }
    79.     }

    80.     public static int[] getIntParameters(HttpServletRequest request, String name, int defaultNum)
    81.     {
    82.         String paramValues[] = request.getParameterValues(name);
    83.         if(paramValues == null || paramValues.length == 0)
    84.             return new int[0];
    85.         int values[] = new int[paramValues.length];
    86.         for(int i = 0; i < paramValues.length; i++)
    87.             try
    88.             {
    89.                 values[i] = Integer.parseInt(paramValues[i].trim());
    90.             }
    91.             catch(Exception e)
    92.             {
    93.                 values[i] = defaultNum;
    94.             }

    95.         return values;
    96.     }

    97.     public static double getDoubleParameter(HttpServletRequest request, String name, double defaultNum)
    98.     {
    99.         String temp = request.getParameter(name);
    100.         if(temp != null && !temp.equals(""))
    101.         {
    102.             double num = defaultNum;
    103.             try
    104.             {
    105.                 num = Double.parseDouble(temp.trim());
    106.             }
    107.             catch(Exception ignored) { }
    108.             return num;
    109.         } else
    110.         {
    111.             return defaultNum;
    112.         }
    113.     }

    114.     public static long getLongParameter(HttpServletRequest request, String name, long defaultNum)
    115.     {
    116.         String temp = request.getParameter(name);
    117.         if(temp != null && !temp.equals(""))
    118.         {
    119.             long num = defaultNum;
    120.             try
    121.             {
    122.                 num = Long.parseLong(temp.trim());
    123.             }
    124.             catch(Exception ignored) { }
    125.             return num;
    126.         } else
    127.         {
    128.             return defaultNum;
    129.         }
    130.     }

    131.     public static long[] getLongParameters(HttpServletRequest request, String name, long defaultNum)
    132.     {
    133.         String paramValues[] = request.getParameterValues(name);
    134.         if(paramValues == null || paramValues.length == 0)
    135.             return new long[0];
    136.         long values[] = new long[paramValues.length];
    137.         for(int i = 0; i < paramValues.length; i++)
    138.             try
    139.             {
    140.                 values[i] = Long.parseLong(paramValues[i].trim());
    141.             }
    142.             catch(Exception e)
    143.             {
    144.                 values[i] = defaultNum;
    145.             }

    146.         return values;
    147.     }

    148.     public static String getAttribute(HttpServletRequest request, String name)
    149.     {
    150.         return getAttribute(request, name, false);
    151.     }

    152.     public static String getAttribute(HttpServletRequest request, String name, boolean emptyStringsOK)
    153.     {
    154.         String temp = (String)request.getAttribute(name);
    155.         if(temp != null)
    156.         {
    157.             if(temp.equals("") && !emptyStringsOK)
    158.                 return null;
    159.             else
    160.                 return temp;
    161.         } else
    162.         {
    163.             return null;
    164.         }
    165.     }

    166.     public static boolean getBooleanAttribute(HttpServletRequest request, String name)
    167.     {
    168.         String temp = (String)request.getAttribute(name);
    169.         return temp != null && temp.equals("true");
    170.     }

    171.     public static int getIntAttribute(HttpServletRequest request, String name, int defaultNum)
    172.     {
    173.         String temp = (String)request.getAttribute(name);
    174.         if(temp != null && !temp.equals(""))
    175.         {
    176.             int num = defaultNum;
    177.             try
    178.             {
    179.                 num = Integer.parseInt(temp.trim());
    180.             }
    181.             catch(Exception ignored) { }
    182.             return num;
    183.         } else
    184.         {
    185.             return defaultNum;
    186.         }
    187.     }

    188.     public static long getLongAttribute(HttpServletRequest request, String name, long defaultNum)
    189.     {
    190.         String temp = (String)request.getAttribute(name);
    191.         if(temp != null && !temp.equals(""))
    192.         {
    193.             long num = defaultNum;
    194.             try
    195.             {
    196.                 num = Long.parseLong(temp.trim());
    197.             }
    198.             catch(Exception ignored) { }
    199.             return num;
    200.         } else
    201.         {
    202.             return defaultNum;
    203.         }
    204.     }
    205. }                     
    复制代码

       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/10/1/210733219.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-22 06:53 , Processed in 0.405052 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表