你说的情况应该是: JAVA是支持多种编码方式的,如果没有指明传送的字符是何种编码,JSP会按默认的ISO8859-1方式,由其是表单提交以及从数据库中存取的时候特别常见。你需要根据情况重新转换一下,可以用 String(str.getBytes(),"GB2312"), 也可以用下面的代码段。 int n = srouceString.length(); byte temp[] = new byte(n); for(int i = 0 ;i<n;i++)temp[i]=sourceString[i]; String result = new String(temp); ---------------------------------------------------------------
解决办法:
第一:
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;
}
}
JAVA是支持多种编码方式的,如果没有指明传送的字符是何种编码,JSP会按默认的ISO8859-1方式,由其是表单提交以及从数据库中存取的时候特别常见。你需要根据情况重新转换一下,可以用 String(str.getBytes(),"GB2312"),
也可以用下面的代码段。
int n = srouceString.length();
byte temp[] = new byte(n);
for(int i = 0 ;i<n;i++)temp[i]=sourceString[i];
String result = new String(temp);
---------------------------------------------------------------
JRun中设置contentType="text/html;charset=gb2312" 只能保证正常显示页面内容
而通过request.getParameter()方法得到的,和从数据库中读出的则显示乱码
这时候则需要进行内码转换
address=new String(request.getParameter("Address").getBytes("8859_1"));
这样就可以取得中文了。
address=new String(request.getParameter("Address").getBytes("8859_1"));
这样就可以了