linux下 tomcat 乱码问题(急,在线等待) 你装Linux时选的什么语言环境?还有你的tomcat是什么语言的?mysql呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在bean裏加入下面這段代码试试,再導入jsp文件中去 /** * 解决中文问题,ISO转为GBK编码,用于POST,GET方式取得数据 * @param str 原始文本 * @return 转码后的文本 */ public String iso2gb(String str) { if (str != null) { byte[] tmpbyte=null; try { tmpbyte=str.getBytes("ISO8859_1"); } catch (UnsupportedEncodingException e) { System.out.println("Error: Method: dbconn.iso2gb :"+e.getMessage()); } try { str=new String(tmpbyte,"GBK"); } catch(UnsupportedEncodingException e) { System.out.println("Error: Method: dbconn.gb2iso :"+e.getMessage()); } } return str; } /** * 解决中文问题,GBK转ISO编码,用于从数据库中存入转码 * @param str 原始文本 * @return 转换后文本 */ public String gb2iso(String str) { if (str != null) { byte[] tmpbyte=null; try { tmpbyte=str.getBytes("GBK"); } catch(UnsupportedEncodingException e) { System.out.println("Error: Method: dbconn.gb2iso :"+e.getMessage()); } try { str=new String(tmpbyte,"ISO8859_1"); } catch(UnsupportedEncodingException e) { System.out.println("Error: Method: dbconn.gb2iso :"+e.getMessage()); } } return str; }} 给你一个字符转换的类,你在活的参数向数据库插入的时候调用,从数据库里取出来的时候不用转换就可。数据库是Oraclepublic class ConvertCharSet{ public static String GBKToISO88591(String str) { if(str == null) return str; try { return new String(str.getBytes("GBK"),"8859_1"); } catch(Exception usex) { return str; } } public static String GBKToGB2312(String str) { if(str == null) return str; try { return new String(str.getBytes("GBK"),"GB2312"); } catch(Exception usex) { return str; } } public static String UnicodeToISO88591(String str) { if(str == null) return str; try { return new String(str.getBytes("iso-8859-1")); } catch(Exception e) { return str; } } public static String ISO88591ToGB2312(String str) { if(str == null) return str; try { return new String(str.getBytes("iso-8859-1"),"GB2312"); } catch(java.io.UnsupportedEncodingException e) { return str; } } public static String ISO88591ToGBK(String str) { if(str == null) return str; try { return new String(str.getBytes("iso-8859-1"),"GBK"); } catch(java.io.UnsupportedEncodingException e) { return str; } } /*public static String UnicodeToGB2312(String str) { if(str == null) return str; try { char[] temp = str.getChars(); byte[] b = new byte[temp.length]; for(int i = 0; i < temp.length;i++) b[i] = (byte)temp[i]; return new String(b,"GB2312"); } catch(Exception e) { return str; } }*/ public static String EscapeHTMLTags(String input) { if(input == null || input.length() == 0) return input; StringBuffer buf = new StringBuffer(input.length() + 6); char ch = ' '; for(int i = 0;i<input.length();i++) { ch = input.charAt(i); if(ch == '<') buf.append("<"); else if(ch == '>') buf.append(">"); else if(ch == '\n') buf.append("<br>"); else if(ch == ' ') buf.append(" "); else if(ch == '\'') buf.append("´"); else buf.append(ch); } return buf.toString(); } public static String toOriginal(String input) { if(input == null || input.length() ==0) return input; //String back = input; while(input.indexOf("<br>") >= 0) { int n = input.indexOf("<br>"); input = input.substring(0,n) + input.substring(n + 4); } return input; } public static String Cut(String s) { if((s == null)||(s.length() < 5)) return "N/A"; int x = s.indexOf("<"); if (x != -1) s = s.substring(0,x) + "..."; return s;}} 补充上面,调用例子:String title = req.getParameter("title");title = ConvertCharSet.EscapeHTMLTags(title);//记录多行文本框中的回车,空格等信息title = ConvertCharSet.ISO88591ToGB2312(title);//字符转换 你用utf-8加字符集filter试一下看看。具体做法是:所有的.jsp文件都用utf-8写一个字符集filter,其实,tomcat中就有现存的。再将你的应用下的web.xml中加入<filter> <filter-name>Set Character Encoding</filter-name> <filter-class>filters.SetCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> 恩。。存入到数据库的时候要改变一下字符编码格式,一般转换成unicode读出来的时候再转换成GBK 因为request默认的字符集是iso-8859-1的所以jsp页面之间、jsp与servlet之间传递数据的时候最好像楼上lhbf(牧野)所说,加个设置request的characterEncoding的filter过滤一下至于与数据库的交互,我一般是在建立连接的时候设置clientEncoding,比如String ur1="jdbc:postgresql://localhost:5432/test?useUnicode=true&characterEncoding=GBK";是的,就是在后面添加了“?useUnicode=true&characterEncoding=GBK”这么一段Good Luck Java教程 ajax获取xml问题 初学Spring,写了一个简单的helloword就遇到了问题。 关于JDBC基础问题 急!关于dom4j的问题? 不同目录下的session问题 请问谁知道:net.sf.hibernate.Hibernate;在哪里下载 配置连接池出错 如何保证servlet画图在重新加载时不重画呢? 类文件我放在web-inf/classes/mydb/mydb.class下,却出现错误 href链接能不能有个参数可以设置每次下载文件都是到web server 取最新文件?? 为什么getparameter取得的是null
/**
* 解决中文问题,ISO转为GBK编码,用于POST,GET方式取得数据
* @param str 原始文本
* @return 转码后的文本
*/
public String iso2gb(String str) {
if (str != null) {
byte[] tmpbyte=null;
try {
tmpbyte=str.getBytes("ISO8859_1");
}
catch (UnsupportedEncodingException e) {
System.out.println("Error: Method: dbconn.iso2gb :"+e.getMessage());
}
try {
str=new String(tmpbyte,"GBK");
}
catch(UnsupportedEncodingException e) {
System.out.println("Error: Method: dbconn.gb2iso :"+e.getMessage());
}
}
return str;
} /**
* 解决中文问题,GBK转ISO编码,用于从数据库中存入转码
* @param str 原始文本
* @return 转换后文本
*/
public String gb2iso(String str) {
if (str != null) {
byte[] tmpbyte=null;
try {
tmpbyte=str.getBytes("GBK");
}
catch(UnsupportedEncodingException e) {
System.out.println("Error: Method: dbconn.gb2iso :"+e.getMessage());
}
try {
str=new String(tmpbyte,"ISO8859_1");
}
catch(UnsupportedEncodingException e) {
System.out.println("Error: Method: dbconn.gb2iso :"+e.getMessage());
}
}
return str;
}}
{
public static String GBKToISO88591(String str)
{
if(str == null) return str;
try
{
return new String(str.getBytes("GBK"),"8859_1");
}
catch(Exception usex)
{
return str;
}
} public static String GBKToGB2312(String str)
{
if(str == null) return str;
try
{
return new String(str.getBytes("GBK"),"GB2312");
}
catch(Exception usex)
{
return str;
}
} public static String UnicodeToISO88591(String str)
{
if(str == null) return str;
try
{
return new String(str.getBytes("iso-8859-1"));
}
catch(Exception e)
{
return str;
}
} public static String ISO88591ToGB2312(String str)
{
if(str == null) return str;
try
{
return new String(str.getBytes("iso-8859-1"),"GB2312");
}
catch(java.io.UnsupportedEncodingException e)
{
return str;
}
} public static String ISO88591ToGBK(String str)
{
if(str == null) return str;
try
{
return new String(str.getBytes("iso-8859-1"),"GBK");
}
catch(java.io.UnsupportedEncodingException e)
{
return str;
}
} /*public static String UnicodeToGB2312(String str)
{
if(str == null) return str;
try
{
char[] temp = str.getChars();
byte[] b = new byte[temp.length];
for(int i = 0; i < temp.length;i++)
b[i] = (byte)temp[i];
return new String(b,"GB2312");
}
catch(Exception e)
{
return str;
}
}*/ public static String EscapeHTMLTags(String input)
{
if(input == null || input.length() == 0)
return input;
StringBuffer buf = new StringBuffer(input.length() + 6);
char ch = ' ';
for(int i = 0;i<input.length();i++)
{
ch = input.charAt(i);
if(ch == '<')
buf.append("<");
else if(ch == '>')
buf.append(">");
else if(ch == '\n')
buf.append("<br>");
else if(ch == ' ')
buf.append(" ");
else if(ch == '\'')
buf.append("´");
else
buf.append(ch);
}
return buf.toString();
} public static String toOriginal(String input)
{
if(input == null || input.length() ==0)
return input;
//String back = input;
while(input.indexOf("<br>") >= 0)
{
int n = input.indexOf("<br>");
input = input.substring(0,n) + input.substring(n + 4);
}
return input;
} public static String Cut(String s)
{
if((s == null)||(s.length() < 5))
return "N/A";
int x = s.indexOf("<");
if (x != -1)
s = s.substring(0,x) + "...";
return s;
}
}
String title = req.getParameter("title");
title = ConvertCharSet.EscapeHTMLTags(title);//记录多行文本框中的回车,空格等信息
title = ConvertCharSet.ISO88591ToGB2312(title);//字符转换
具体做法是:
所有的.jsp文件都用utf-8
写一个字符集filter,其实,tomcat中就有现存的。
再将你的应用下的web.xml中加入
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
存入到数据库的时候要改变一下字符编码格式,一般转换成unicode
读出来的时候再转换成GBK
所以jsp页面之间、jsp与servlet之间传递数据的时候最好像楼上lhbf(牧野)所说,加个设置request的characterEncoding的filter过滤一下至于与数据库的交互,我一般是在建立连接的时候设置clientEncoding,比如
String ur1="jdbc:postgresql://localhost:5432/test?useUnicode=true&characterEncoding=GBK";是的,就是在后面添加了“?useUnicode=true&characterEncoding=GBK”这么一段Good Luck