三度网教程:是一个免费提供流行视频软件教程、在线学习分享的学习平台!

Java Servlet/Jsp多语言处理方案(二)

时间:2022-7-19作者:未知来源:三度网教程人气:


我是这么做的,建立一个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多语言处理方案(二)





Copyright © 2012-2018 三度网教程(http://www.3du8.cn) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版