[摘要]我是这么做的,建立一个Servlet基类,覆盖service方法,在调用父类的service方 法前读取并解析form提交的内容,请看下面的源代码: package com.hto.servlet;... 我是这么做的,建立一个Servlet基类,覆盖service方法,在调用父类的service方 法前读取并解析form提交的内容,请看下面的源代码: package com.hto.servlet; import javax.servlet.http.HttpServletRequest; import java.util.*; /** * Insert the type's description here. * Creation date: (2001-2-4 15:43:46) * @author: 钱卫春 */ public class UTF8ParameterReader { Hashtable pairs = new Hashtable(); /** * UTF8ParameterReader constructor comment. */ public UTF8ParameterReader(HttpServletRequest request) throws java.io.IOExce ption{ super(); parse(request.getQueryString()); parse(request.getReader().readLine()); } /** * UTF8ParameterReader constructor comment. */ public UTF8ParameterReader(HttpServletRequest request,String encoding) throw s java.io.IOException{ super(); parse(request.getQueryString(),encoding); parse(request.getReader().readLine(),encoding); } public static String decode(String s) throws Exception { StringBuffer sb = new StringBuffer(); for(int i=0; i<s.length(); i++) { char c = s.charAt(i); switch (c) { case '+': sb.append(' '); break; case '%': try { sb.append((char)Integer.parseInt( s.substring(i+1,i+3),16)); } catch (NumberFormatException e) { throw new IllegalArgumentException(); } i += 2; break; default: sb.append(c); break; } } // Undo conversion to external encoding String result = sb.toString(); byte[] inputBytes = result.getBytes("8859_1"); return new String(inputBytes,"UTF8"); } public static String decode(String s,String encoding) throws Exception { StringBuffer sb = new StringBuffer(); for(int i=0; i<s.length(); i++) { char c = s.charAt(i); switch (c) { case '+': sb.append(' '); break; case '%': try { sb.append((char)Integer.parseInt( s.substring(i+1,i+3),16)); } catch (NumberFormatException e) { throw new IllegalArgumentException(); } i += 2; break; default: sb.append(c); break; } } // Undo conversion to external encoding String result = sb.toString(); byte[] inputBytes = result.getBytes("8859_1"); return new String(inputBytes,encoding); } /** * Insert the method's description here. * Creation date: (2001-2-4 17:30:59) * @return java.lang.String * @param name java.lang.String */ public String getParameter(String name) { if (pairs == null |
关键词: Java Servlet/Jsp多语言处理方案(二)