JSP亂碼總結和自訂Filter設定編碼

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.