老生长叹:JSP传递中文到SERVLET发现乱码!

dgcat_6 2005-04-05 02:26:50
事情是这样的:以JSP开发WAP程序,为了通用性。采用UTF-8编码。可是把参数提交并经过SERVLET处理后,在下一个页面显示为乱码!所有的JSP统一采用UTF-8编码。并没有做任何的转码。可是我在控制台打印出是正常的:
比如:
String s=request.getParameter("userName");
System.out.println(s); //汉字正常
用页面输出则错误:
out.println(s);
为什么会这样?经HTTP协议传递的参数是采用什么编码了?是JSP指定的页面编码还是服务器端操作系统编码还是其他?对了
我的WEB应用是在Linux+WEBLOIGC
...全文
181 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
dgcat_6 2005-04-05
  • 打赏
  • 举报
回复
顺便问问:ISO8859_1 和 ISO-8859-1有什么区别?
dgcat_6 2005-04-05
  • 打赏
  • 举报
回复
万分感谢
dgcat_6 2005-04-05
  • 打赏
  • 举报
回复
jfy3d(剑事) :你的类真是太强大了!用getEncoding 方法判断出是ISO-8859-1编码!搞定了!你的这个类收藏!
seakingwy 2005-04-05
  • 打赏
  • 举报
回复
恩 试试在!
dgcat_6 2005-04-05
  • 打赏
  • 举报
回复
好!我试试!
剑事 2005-04-05
  • 打赏
  • 举报
回复
package com;

public class TranCharset {

private static final String PRE_FIX_UTF = "&#x";
private static final String POS_FIX_UTF = ";";

public TranCharset() {
}

/**
* Translate charset encoding to unicode
*
* @param sTemp charset encoding is gb2312
* @return charset encoding is unicode
*/
public static String XmlFormalize(String sTemp) {
StringBuffer sb = new StringBuffer();

if (sTemp == null || sTemp.equals("")) {
return "";
}
String s = TranCharset.TranEncodeTOGB(sTemp);
for (int i = 0; i < s.length(); i++) {
char cChar = s.charAt(i);
if (TranCharset.isGB2312(cChar)) {
sb.append(PRE_FIX_UTF);
sb.append(Integer.toHexString(cChar));
sb.append(POS_FIX_UTF);
} else {
switch ((int) cChar) {
case 32:
sb.append(" ");
break;
case 34:
sb.append(""");
break;
case 38:
sb.append("&");
break;
case 60:
sb.append("<");
break;
case 62:
sb.append(">");
break;
default:
sb.append(cChar);
}
}
}
return sb.toString();
}

/**
* 将字符串编码格式转成GB2312
* @param str
* @return
*/
public static String TranEncodeTOGB(String str) {
try {
String strEncode = TranCharset.getEncoding(str);
String temp = new String(str.getBytes(strEncode), "GB2312");
return temp;
} catch (java.io.IOException ex) {

return null;
}
}

/**
* 判断输入字符是否为gb2312的编码格式
*
* @param c 输入字符
* @return 如果是gb2312返回真,否则返回假
*/
public static boolean isGB2312(char c) {
Character ch = new Character(c);
String sCh = ch.toString();
try {
byte[] bb = sCh.getBytes("gb2312");
if (bb.length > 1) {
return true;
}
} catch (java.io.UnsupportedEncodingException ex) {
return false;
}
return false;
}

/**
* 判断字符串的编码
*
* @param str
* @return
*/
public static String getEncoding(String str) {
String encode = "GB2312";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s = encode;
return s;
}
} catch (Exception exception) {
}
encode = "ISO-8859-1";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s1 = encode;
return s1;
}
} catch (Exception exception1) {
}
encode = "UTF-8";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s2 = encode;
return s2;
}
} catch (Exception exception2) {
}
encode = "GBK";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s3 = encode;
return s3;
}
} catch (Exception exception3) {
}
return "";
}
}

不管是数据库里读出来的还是 传过来的 用这个转一下再out.println
dgcat_6 2005-04-05
  • 打赏
  • 举报
回复
楼上老兄:我是做WAP应用!因为手机基本上都是采用UTF8,所以我的JSP页面也全部采用UTF8!从浏览器和手机查看效果,预先在页面设置的中文都正确。只是有几个环节需要传递中文的地方出现了乱码!

TO: huguangwu(追风少年)
你的方法试过,还是不行!
剑事 2005-04-05
  • 打赏
  • 举报
回复
你做的是wap 如果你用gbk 电脑浏览不乱码,手机浏览的话会乱码 手机是utf8
dgcat_6 2005-04-05
  • 打赏
  • 举报
回复
To:jfy3d(剑事)
out.println("用户");
这样直接对输出的中文进行编码不容易维护,而且有些中文根本不能确定。
TO:minisun2000(天生不专一):
经HTTP协议传递的参数是采用什么编码了?ISO8859_1 编码,你确定吗?不过到是linux 是采用 ISO8859_1 编码的。我等会试试!多谢各位!
wtiancai 2005-04-05
  • 打赏
  • 举报
回复
http://www.cnblogs.com/wtiancai/archive/2005/04/04/131891.html
剑事 2005-04-05
  • 打赏
  • 举报
回复
WAP
response.setContentType(""text/vnd.wap.wml"");
request.setCharacterEncoding("ISO-8859-1");
java.io.PrintWriter out = response.getWriter();
out.println("用户");


用户这样的中文
剑事 2005-04-05
  • 打赏
  • 举报
回复
response.setContentType("text/html;charset=GB2312");
request.setCharacterEncoding("ISO-8859-1");
java.io.PrintWriter out = response.getWriter();
out.println(中文);
minisun2000 2005-04-05
  • 打赏
  • 举报
回复
经HTTP协议传递的参数是采用什么编码了?
ISO8859_1
huguangwu 2005-04-05
  • 打赏
  • 举报
回复
String s=new String(request.getParameter("userName").getBytes("ISO8859_1"),"GB2312").toString()

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧