Excaliburer`s Zone

It was challenging, but not risky.

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

[转]解决汉字乱码:处理汉字信息Charset和charset

解决汉字乱码:处理汉字信息Charset和charset

转自:http://hi.baidu.com/wicom/item/6ca20ca941df7c736dd455da

一)当使用request对象获取客户端提交的汉字字符时,会出现乱码问题,所以对含有汉字符的信息必须进行特殊的处理。使用request对象的JSP页面可以使用两种方式避免出现汉字乱码问题:

一种方式使用page指定contentType属性的值时,做如下指定

<%@page contentType="text/html;Charset="GB2312"%>即将其中出现的Charset中的首写字母C大写。

这时通过request对象直接取值即可。

另一种方式把c小写如下:<%@page contentType="text/html;charset="GB2312"%>

这时需要将通过内置对象request获取的信息重新编码一次,即用ISO-8859-1进行编码一次,放入字节数组中,然后转换成字符串即可。

String str=request.getParameter("message");

byte b[] = str.getBytes("ISO-8859-1");

str = new String(b);

编码总结:

(1) 对于采用gb2312或utf-8等编码的页面进行传送参数时,接收页面,采用大写Charset时采用对应编码时不需转换编码,即可。但是对于utf- 8编码的只能采用上述大写Charset方式,对于gb2312传送的,可以接收页面可以采用charset,不过这时需要重新采用iso-8859-1 编码。

(2)因此简单起见,接受参数的页面设置直接都设置成大写Charset。

二)采用utf-8编码

submit.jsp

<%@ page contentType="text/html;charset=utf-8" %>
<html>
<head>
<title>本页面采用charset=utf-8</title>
</head>
<body>
<form action="charset.jsp" method="post">
<input type="text" name="boy"><input type="submit" value="提交给charset.jsp" name="submit">
</form>
<form action="Charsetd.jsp" method="post">
<input type="text" name="boy"><input type="submit" value="提交给Charsetd.jsp" name="submit">
</form>
</body>
</html>

Charsetd.jsp页面,采用大写Charset,显示为正常,不需中间转码

<%@ page contentType="text/html; Charset=utf-8" %>
<html>
<head>
<title>正常显示,但是如果Charset写成charset--C小写,则获得参数为乱码。即使使用utf-8重新编码也为乱码</title>
</head>
<body>
<%=request.getParameter("boy")%><br>
<%=request.getParameter("submit")%>
</body>
</html>

charset.jsp 本页面采用小写charset,全显示为乱码

<%@ page contentType="text/html; charset=utf-8" %>
<html>
<head>
<title>本页面采用charset小写,从新编码,但全为乱码</title>
</head>
<body>
<%String boy = request.getParameter("boy");
String submit = request.getParameter("submit");
byte b[] = boy.getBytes("utf-8");
String st1 = new String(b);
out.println("boy "+st1+" "+new String(st1.getBytes("ISO-8859-1")));
out.println(boy+" "+submit);
out.println("boy "+new String(b));
b = submit.getBytes("ISO-8859-1");
String st2 = new String(b);
out.println("submit"+st2+" "+new String(st2.getBytes("utf-8")));
%>
</body>
</html>

三)采用gb2312编码

1.submit.jsp

<%@ page contentType="text/html;charset=gb2312" %>
<html>
<head>
<title>本页面采用charset=gb2312</title>
</head>
<body>
<form action="charset.jsp" method="post">
<input type="text" name="boy"><input type="submit" value="提交给charset.jsp" name="submit">
</form>
<form action="Charsetd.jsp" method="post">
<input type="text" name="boy"><input type="submit" value="提交给Charsetd.jsp" name="submit">
</form>
</body>
</html>

2.Charset.jsp

<%@ page contentType="text/html; Charset=gb2312" %>
<html>
<head>
<title>本页面采用大写Charset设置,正常显示,若采用小写charset则为乱码</title>
</head>
<body>
<%=request.getParameter("boy")%><br>
<%=request.getParameter("submit")%>
</body>
</html>
3.charset.jsp

<%@ page contentType="text/html; charset=utf-8" %>
<html>
<head>
<title>本页面采用charset小写,ISO-8859-1从新编码后正常显示</title>
</head>
<body>
<%String boy = request.getParameter("boy");
String submit = request.getParameter("submit");
byte b[] = boy.getBytes("ISO-8859-1");
String st1 = new String(b);
out.println("boy "+st1);
b = submit.getBytes("ISO-8859-1");
String st2 = new String(b);
out.println("submit "+st2);
%>
</body>
</html>

posted on 2014-05-19 15:57  Excaliburer  阅读(601)  评论(0)    收藏  举报