我用jsp做的一个东东,为什么汉字是乱码?? 这个问题我遇见过,需要加一段代码,具体是什么我也不记得了,是一本很厚的JSP书上写的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在request.getParameter()之前加一句request.setCharacterEncoding("gb2312");就好了~~~~ 用转换函数处理request.getParameter("")<%String para = getChinese(request.getParameter("para"))%><%!public String getChinese(String str) { try{ String temp_p=str; byte[] temp_t=temp_p.getBytes("ISO8859-1"); String temp=new String(temp_t); return temp; } catch (Exception e){ e.printStackTrace(); } return "null";}%> strTemp=new String(strTemp.getBytes("ISO8859-1"), "GB2312"); package com;import java.io.*;public class Chinacode{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; }}} /** * 将一个unicode字符串转换成GB2312中文字符串 * * @param strUnicode unicode字符串 * @return GB2312 */ static public String UnicodeToChinese(String strUnicode) { String strChinese = ""; try { if ((strUnicode != null) && (strUnicode.length() > 0)) { strChinese = new String(strUnicode.getBytes("ISO-8859-1"), "GB2312"); } } catch (java.io.UnsupportedEncodingException e) { common.util.Log.print("Unicode to Chinese: " + e.getMessage()); strChinese = ""; } return strChinese; } /** * 将一个GB2312中文字符串转换成unicode字符串 * * @param strChinese 中文字符串 * @return ISO-8859-1 */ static public String ChineseToUnicode(String strChinese) { String strUnicode = ""; try { if ((strChinese != null) && (strChinese.length() > 0)) { strUnicode = new String(strChinese.getBytes("GB2312"), "ISO-8859-1"); } } catch (java.io.UnsupportedEncodingException e) { common.util.Log.print("Chinese to Unicode: " + e.getMessage()); strUnicode = ""; } return strUnicode; }jsp->数据库用ChineseToUnicode()方法转码,数据库->jsp用UnicodetoChinese()方法转码. String value=request.getParameter("name");value=new String(value.getBytes("8859_1")); 需要经过转换String str = request.getParameter("sssss");if(str!=null){ str = new String(str.getBytes("8859_1");}else{ str = "";} at your database connection set encode to "gb2312" 向JSP页面引入自定义类,求救; ssh2 项目初始化问题 使用cookie的超奇怪问题 我的tomcat怎么认得*.jsp???? 有关java mail 寻求方案“Web实时对话” 紧急求助:连接sql server数据库的问题,快帮帮我啊 jsp联动的select怎么取数据? linux系统下的jsp中文显示乱码问题 求达人给一个简单理解单文件上传到FTP的JSP例子,没有SERVLET的 jsp中,如何得到上一页的地址 WML中的问题 !!!!!!!!!!!!!1
request.setCharacterEncoding("gb2312");
就好了~~~~
<%
String para = getChinese(request.getParameter("para"))
%><%!
public String getChinese(String str) {
try{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
}
catch (Exception e){
e.printStackTrace();
}
return "null";
}
%>
import java.io.*;public class Chinacode
{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;
}
}
}
* 将一个unicode字符串转换成GB2312中文字符串
*
* @param strUnicode unicode字符串
* @return GB2312
*/
static public String UnicodeToChinese(String strUnicode)
{
String strChinese = "";
try {
if ((strUnicode != null) && (strUnicode.length() > 0)) {
strChinese = new String(strUnicode.getBytes("ISO-8859-1"),
"GB2312");
}
} catch (java.io.UnsupportedEncodingException e) {
common.util.Log.print("Unicode to Chinese: " + e.getMessage());
strChinese = "";
} return strChinese;
} /**
* 将一个GB2312中文字符串转换成unicode字符串
*
* @param strChinese 中文字符串
* @return ISO-8859-1
*/
static public String ChineseToUnicode(String strChinese)
{
String strUnicode = "";
try {
if ((strChinese != null) && (strChinese.length() > 0)) {
strUnicode = new String(strChinese.getBytes("GB2312"),
"ISO-8859-1");
}
} catch (java.io.UnsupportedEncodingException e) {
common.util.Log.print("Chinese to Unicode: " + e.getMessage());
strUnicode = "";
} return strUnicode;
}
jsp->数据库用ChineseToUnicode()方法转码,
数据库->jsp用UnicodetoChinese()方法转码.
value=new String(value.getBytes("8859_1"));
if(str!=null){
str = new String(str.getBytes("8859_1");
}
else{
str = "";
}