实用的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 }
原文地址:https://www.cnblogs.com/liyangxj/p/4118152.html