本人对java,jsp等相关网页只是很不熟悉,今日为了修改别人留下来的java后台html中嵌套jsp做界面的项目中的一些内容受尽了折磨。在text或者textarea中传入中文的时候,getParameter方法总是得到一堆?,无人可问,只好在网上盲目的找。网上有好多更改jsp编码的方法,也有很多在java中对编码进行各种转换的,试了又试,没什么用。或许可以成功吧,但是我的情况是,当前jsp引用了别的jsp,然而别的jsp从gbk变成utf-8之后就报了html-500的错误,为了不引起更多的未知错误,我放弃了修改界面编码的方向的尝试。而后在CSDN的一帖子中高分的18楼的解决方法帮助了我,为了避免忘记,本人将该帖子引用至此,地址http://bbs.csdn.net/topics/390360298。我们再来看一看这个神奇的类:
1 public class HttpRequestReader { 2 3 Hashtable pairs = new Hashtable(); 4 5 /** 6 * RawParameterReader constructor comment. 7 */ 8 public HttpRequestReader(HttpServletRequest request, String encoding) 9 throws IOException { 10 super(); 11 parse(request.getQueryString(), encoding); 12 parse(request.getReader().readLine(), encoding); 13 } 14 15 public static String decode(String s, String encoding) throws Exception { 16 StringBuffer sb = new StringBuffer(); 17 for (int i = 0; i < s.length(); i++) { 18 char c = s.charAt(i); 19 switch (c) { 20 case ‘+‘: 21 sb.append(‘ ‘); 22 break; 23 case ‘%‘: 24 try { 25 sb.append((char) Integer.parseInt( 26 s.substring(i + 1, i + 3), 16)); 27 } catch (NumberFormatException e) { 28 throw new IllegalArgumentException(); 29 } 30 i += 2; 31 break; 32 default: 33 sb.append(c); 34 break; 35 } 36 } 37 // Undo conversion to external encoding 38 String result = sb.toString(); 39 byte[] inputBytes = result.getBytes("8859_1"); 40 return new String(inputBytes, encoding); 41 } 42 43 /** 44 * Insert the method‘s description here. Creation date: (2001-2-4 17:30:59) 45 * 46 * @return java.lang.String 47 * @param name 48 * java.lang.String 49 */ 50 public String getParameter(String name) { 51 if (pairs == null || !pairs.containsKey(name)) 52 return null; 53 return (String) (((ArrayList) pairs.get(name)).get(0)); 54 } 55 56 /** 57 * Insert the method‘s description here. Creation date: (2001-2-4 17:28:17) 58 * 59 * @return java.util.Enumeration 60 */ 61 public Enumeration getParameterNames() { 62 if (pairs == null) 63 return null; 64 return pairs.keys(); 65 } 66 67 /** 68 * Insert the method‘s description here. Creation date: (2001-2-4 17:33:40) 69 * 70 * @return java.lang.String[] 71 * @param name 72 * java.lang.String 73 */ 74 public String[] getParameterValues(String name) { 75 if (pairs == null || !pairs.containsKey(name)) 76 return null; 77 ArrayList al = (ArrayList) pairs.get(name); 78 String[] values = new String[al.size()]; 79 for (int i = 0; i < values.length; i++) 80 values[i] = (String) al.get(i); 81 return values; 82 } 83 84 /** 85 * Insert the method‘s description here. Creation date: (2001-2-4 20:34:37) 86 * 87 * @param urlenc 88 * java.lang.String 89 */ 90 private void parse(String urlenc, String encoding) 91 throws java.io.IOException { 92 if (urlenc == null) 93 return; 94 StringTokenizer tok = new StringTokenizer(urlenc, "&"); 95 try { 96 while (tok.hasMoreTokens()) { 97 String aPair = tok.nextToken(); 98 int pos = aPair.indexOf("="); 99 String name = null; 100 String value = null; 101 if (pos != -1) { 102 name = decode(aPair.substring(0, pos), encoding); 103 value = decode(aPair.substring(pos + 1), encoding); 104 } else { 105 name = aPair; 106 value = ""; 107 } 108 if (pairs.containsKey(name)) { 109 ArrayList values = (ArrayList) pairs.get(name); 110 values.add(value); 111 } else { 112 ArrayList values = new ArrayList(); 113 values.add(value); 114 pairs.put(name, values); 115 } 116 } 117 } catch (Exception e) { 118 throw new java.io.IOException(e.getMessage()); 119 } 120 } 121 }
在我引入到我的java工程之后又添加了已下的包:
1 import java.io.IOException; 2 import java.util.ArrayList; 3 import java.util.Enumeration; 4 import java.util.Hashtable; 5 import java.util.StringTokenizer; 6 7 import javax.servlet.http.HttpServletRequest;
以便可以使用。
在需要的地方做已下应用:
1 public void save(HttpServletRequest req, HttpServletResponse resp) 2 throws ServletException, IOException { 3 try { 4 HttpRequestReader paramReader = new HttpRequestReader(req, "gbk"); 5 String strAlertMsg = paramReader.getParameter("device.AlertMsg"); 6 } catch (Exception e) { 7 this.findForward("/device-details.jsp", true, req, resp); 8 return; 9 } 10 this.findForward("/device-details.jsp", true, req, resp); 11 return; 12 }
然后就OK啦!
时间: 2024-10-16 18:45:40