1.自己寫個Filter
CharsetEncodingFilter.java
package filter;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;public class CharsetEncodingFilter implements Filter {private String encoding;public void destroy() {System.out.println("--------destroy---------");}public void doFilter(ServletRequest arg0, ServletResponse arg1,FilterChain arg2) throws IOException, ServletException {System.out.println("--------doFilter---------");arg0.setCharacterEncoding(encoding);arg2.doFilter(arg0,arg1);}public void init(FilterConfig arg0) throws ServletException {System.out.println("--------init---------");this.encoding = arg0.getInitParameter("encoding");System.out.println(encoding);}}
web.xml配置
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter><filter-name>CharsetEncodingFilter</filter-name><filter-class>filter.CharsetEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>CharsetEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping> <filter-mapping><filter-name>CharsetEncodingFilter</filter-name><url-pattern>/servlet/*</url-pattern></filter-mapping></web-app>
2.在頁面
<%@ page contentType="text/html;charset=UTF-8" %><%request.setCharacterEncoding("UTF-8");%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%response.setCharacterEncoding("UTF-8");%>
3.如果出現類似類似這樣的亂碼:%E4%B8%8E%E6%A8%A1%E5%BC%8F%
<%URLDecoder.decode(str,"UTF-8");%>
4.自己手動編碼轉換
String str = (String) request.getParameter("username");byte[] temp = str.getBytes("ISO-8859-1");str = new String(temp,"UTF-8");out.print(str);
5.修改tomcat預設編碼
預設情況下,tomcat使用的是iso8859-1的編碼編碼方式
修改tomcat下的conf/server.xml檔案
找到如下代碼:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
這段代碼規定了Tomcat監聽HTTP請求的連接埠號碼等資訊。
可以在這裡添加一個屬性:URIEncoding,將該屬性值設定為UTF-8,即可讓Tomcat(預設ISO-8859-1編碼)以UTF-8的編碼處理get請求。
更改後的代碼如下所示:
<Connector port="8080" URIEncoding="UTF-8" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />此外
還可以增加屬性useBodyEncodingForURI="true" 設定POST和GET使用相同編碼
更改後的代碼如下所示:
<Connector port="8080" useBodyEncodingForURI="true" URIEncoding="UTF-8" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
6.注意response.setContentType("text/html;charset=utf-8");和PrintWriter out = response.getWriter();的位置關係,
切記要將PrintWriter out = response.getWriter();放在response.setContentType("text/html;charset=utf-8");的後面,否則設定的編碼將無效
7.其他
http://fiona777.iteye.com/blog/385934
http://handonghandong.iteye.com/blog/758866
http://gaobaolu.blog.edu.cn/2012/722247.html