试试:str1=new String(str.getBytes(),"GBK"); 其实在插入时,插入转换成ascii,
new Stirng(someString.getBytes("GBK"),"iso-8859-1")
取出时,
new String(someString.getBytes("iso-8859-1"),"GBK")
就该没问题了。
new Stirng(someString.getBytes("GBK"),"iso-8859-1")
取出时,
new String(someString.getBytes("iso-8859-1"),"GBK")
就该没问题了。
<% @ page contentType="text/html;charset=gb2312"%>
<%request.setCharacterEncoding("gb2312");%>
给分啊!!给分啊!!给分啊!!给分啊!!给分啊!!给分啊!!给分啊!!给分啊!!给分啊!!给分啊!!给分啊!!给分啊!!给分啊!!给分啊!!给分啊!!给分啊!!给分啊!!给分啊!!给分啊!!给分啊!!
给分啊!!给分啊!!给分啊!!给分啊!!给分啊!!给分啊!!给分啊!!给分啊!!
第一:
1:在jsp页面加入:
<%@ page contentType="text/html; charset=gb2312" %>
2:在servlet里面:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=gb2312");//这是重要的
3:上面的如果在不行就用如下的方法在数据入库前进行调用:
public static String UnicodeToChinese(String s){
try{
if(s==null ¦ ¦s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("ISO8859-1"),"gb2312");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}
public static String ChineseToUnicode(String s){
try{
if(s==null ¦ ¦s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("gb2312"),"ISO8859-1");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}
注意:ISO-8859-1
然后重起。一切就都正常。
2:在jsp页中加入一条语句:
<%@ page contentType="text/html;charset=gb2312" %>
Jsp显示就正常了。
3:在编译servlet和jsp时加入代码选项。编译servlet使用
javac -encoding iso8859_1 myservlet.java
在jsp的zone配置文件中.修改编译参数为:
compiler=builtin-javac -encoding ISO8859_1
使用这种方法后,不需要作其他的改动就可以正常显示中文了。
只是看前面网友的文章在编译servlet时都用GBK码,我试了很多
次都不行。而且使用ISO8859_1在原理上难以理解。烦请那位网友给讲讲。
4:最土的办法,在servlet源程序中加入代码变换语句。如
try{
out.println(new ( (new String("我爱死你了")).getBytes("GBK"),"ISO8859_1"))
}
catch( UnsupportedEncodingException e)
{
.......
}
使用这种方法一定要注意捕获UnsupportedEncodingException这个异常。