1.Jsp输出中文乱码.
1).jsp页面头部加语言<%@page text/html;charset=utf-8%>
Servlet中response.SetContentType("text/html;charset=utf-8");
补充:jsp页面head部分:
<meta http-equiv="Content-Type" cntent="text/html;charset=utf-8">
2).在每次要输出中文的地方主动转换编码方式,比如:
<%
String str= "中文";
byte[] tmpbyte = str.getBytes("ISO8859-1");
str = new String(tmpbyte);
out.printl(str);
%>
2.获取表单提交的数据时的中文乱码.
1).将表单中取出的数据进行转码.
String str = request.getParameter("chstr");
string str = new String(str.getBytes("Iso8859-1"),"utf-8");
2).作Filter,写web.xml
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>ch2.util.filter.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf8<param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Tomcat自带的例子中有SetCharacterEncodingFilter.class的源码.
利用:request.setCharacterEncoding(encoding);
3.URL中乱码
如:http://localhost:a.jsp?str=中文
解决方案:
Tomcat安装目录下:/conf/Server.xml文件
找到Connecter块,往其添加URIEncoding="utf-8"/>
4.数据库访问时乱码
数据库连接字符串中:URL
添加:UseUnicode = true&characterEncoding=utf-8
读取时:charConvert(rs.getString("colName"));