zoukankan      html  css  js  c++  java
  • 实用的request接收值的工具类

      1  
      2 import java.io.UnsupportedEncodingException;
      3 
      4 import javax.servlet.http.HttpServletRequest;
      5 
      6 
      7 public class ParamUtil {
      8      
      9     
     10     public static String getGBKStr(HttpServletRequest request, String s) {
     11         String str = "";
     12         try {
     13              String temp = request.getParameter(s);
     14              if (temp == null)
     15                  return str;
     16              else
     17                  str = ISO2GBK(temp.trim());
     18         } catch (Exception e){}
     19         return str;
     20     }
     21     
     22     public static String getString(HttpServletRequest request, String s) {
     23         String str = "";
     24         try {
     25             String temp = request.getParameter(s).trim();            
     26             if (temp == null)
     27                 return str;
     28             else
     29                 str = temp;
     30         } catch (Exception e){}
     31         return str;
     32     }
     33 
     34     
     35     
     36     public static String getStringGBK(HttpServletRequest request, String s) {
     37         String str = "";
     38         try {
     39              String temp = ISO2GBK(request.getParameter(s).trim());
     40              if (temp == null)
     41                  return str;
     42              else
     43                  str = temp;
     44         } catch (Exception e){}
     45         return str;
     46     }
     47 
     48 
     49     
     50     public static String getString2(HttpServletRequest request, String s) {
     51         String str = " ";
     52         try {
     53              String temp = request.getParameter(s);
     54              if (temp == null)
     55                  return str;
     56              else
     57                  str = temp;
     58         } catch (Exception e){}
     59         return str;
     60     }
     61     
     62     public static int getInt(HttpServletRequest request,String s) {
     63         int i = 0;
     64         try {
     65              String temp = getString(request,s);
     66              if (temp.equals(""))
     67                  return i;
     68              else
     69                  i = Integer.parseInt(temp);
     70         } catch (NumberFormatException e) {}
     71         return i;
     72     }
     73     
     74     public static long getLong(HttpServletRequest request,String s) {
     75         long l = 0;
     76         try {
     77              String temp = getString(request,s);
     78              if (temp.equals(""))
     79                  return l;
     80              else
     81                  l = Long.parseLong(temp);
     82         } catch (NumberFormatException e) {}
     83         return l;
     84     }
     85 
     86     public static float getFloat(HttpServletRequest request,String s) {
     87         float f = 0.0f;
     88         try {
     89              String temp = getString(request,s);
     90              if (temp.equals(""))
     91                  return f;
     92              else
     93                  f = Float.parseFloat(temp);
     94         } catch (NumberFormatException e) {}
     95         return f;
     96     }
     97     
     98     public static double getDouble(HttpServletRequest request,String s) {
     99         double d = 0;
    100         try {
    101              String temp = getString(request,s);
    102              if (temp.equals(""))
    103                  return d;
    104              else
    105                  d = Double.parseDouble(temp);
    106         } catch (NumberFormatException e) {}
    107         return d;
    108     }
    109     public static String[] getValues(HttpServletRequest request,String s) {
    110         return request.getParameterValues(s);
    111     }
    112     
    113     public static String ISO2GBK(String s) {
    114         if (s == null){
    115             s = "";
    116         } else {
    117             try {
    118                 s = new String(s.getBytes("ISO-8859-1"),"GBK");
    119             } catch(UnsupportedEncodingException e) {
    120                 System.out.println("转码成功");
    121             }
    122         }
    123         return s;
    124     }
    125     
    126 }
  • 相关阅读:
    MySQL 中的 3 种注释
    Macbook 彻彻底底的卸载MySQL
    MacBook 安装 MySQL 5.7.29(新手都看得懂的安装教程)
    Java Junit单元测试
    理解 Java 方法引用(方法引用符:“双冒号 :: ”)
    iOS应用启动时间
    iOS遍历数组的同时删除元素
    Xcode 中的断言
    Mac 下 查看 使用某端口的进程和关闭该进程的命令
    RAC 数据库的启动与关闭
  • 原文地址:https://www.cnblogs.com/liyangxj/p/4118152.html
Copyright © 2011-2022 走看看