实用的request接收值的工具类

本文介绍了一个用于Java Web项目的参数处理工具类,该工具类能够从HttpServletRequest中获取各种类型的参数,包括字符串、整数、浮点数等,并提供了字符集转换功能。
  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 }

 

转载于:https://www.cnblogs.com/liyangxj/p/4118152.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值