天天看點

如何配置Filter過濾器處理JSP中文亂碼

鍙傝€僒omcat鏈嶅姟鍣ㄧ洰褰晈ebapps鐨別xamples绀轟緥

绠€鍗曢厤缃楠わ細

1.鍦ㄩ」鐩畐eb.xml鏂囦歡娣誨姞杩囨護鍣ㄦ爣璁?lt;filter>鍜?lt;filter-mapping>锛?

2.瀹炵幇杩囨護鍣ㄤ唬鐮侊紱

3.瀵筎omcat鏈嶅姟鍣╟onf鐩綍閲岀殑Server.xml鏂囦歡閰嶇疆URIEncoding锛?

4.鍓嶅彴椤甸潰璁劇疆contentType鐨刢harset鍊間笌web.xml閲岃缃殑鍊間竴鑷淬€?

璇︾粏閰嶇疆姝ラ锛? 1.閰嶇疆web.xm鏂囦歡锛屽湪web-app鏍囩鍐呮坊鍔犺繃婊ゅ櫒鐨勯厤缃?lt;filter>鍜?lt;filter-mapping>

聽 <!--涓枃涔辯爜澶勭悊杩囨護--> 聽
聽聽 聽<filter>
聽聽 聽聽聽 聽<filter-name>EncodingFilter</filter-name><!--杩囨護鍣ㄥ悕绉幫紝鑷繁璧?-->
聽聽 聽聽聽 聽<filter-class>com.filters.SetCharacterEncodingFilter</filter-class><!--杩囨護鍣ㄧ被鍚嶇О锛屽寘鎷寘鍚嶄竴鑷淬€佸啓鍏?-->
聽聽 聽聽聽 聽<init-param><!--鍒濆鍖栧弬鏁?瑕佹寚瀹氱殑瀛楃闆嗙紪鐮?-->
聽聽 聽聽聽 聽聽聽 聽<param-name>encoding</param-name><!--鍙傛暟鍚嶇О -->
聽聽 聽聽聽 聽聽聽 聽<param-value>GBK</param-value><!--鍙傛暟鍊?-->
聽聽 聽聽聽 聽</init-param>
聽聽 聽聽聽 聽<init-param><!--鍒濆鍖栧弬鏁?鎸囧畾鏄惁蹇界暐澶у皬鍐?-->
聽聽 聽聽聽 聽聽聽 聽<param-name>ignore</param-name>
聽聽 聽聽聽 聽聽聽 聽<param-value>true</param-value>
聽聽 聽聽聽 聽</init-param>
聽聽 聽</filter>
聽聽 聽<filter-mapping>
聽聽 聽聽聽 聽<filter-name>EncodingFilter</filter-name>
聽聽 聽聽聽 聽<url-pattern>/*</url-pattern> <!--鈥?*鈥濊〃绀洪」鐩笅鎵€鏈夎祫婧?-->
聽聽 聽</filter-mapping>
聽 <!--涓枃涔辯爜澶勭悊杩囨護 -->聽 
           

2.瀹炵幇杩囨護鍣ㄤ唬鐮侊紝鍒涘緩涓€涓猄etCharacterEncodingFilter绫?

package com.filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * 鑷畾涔夊瓧绗﹀鐞嗚繃婊ゅ櫒
 * 鍙傝€冭嚜Tomcat鐩綍webapps\examples\WEB-INF\classes\filters
 * @author coderlu
 * @since 2012-12-10
 */
public class SetCharacterEncodingFilter implements Filter {

    protected FilterConfig filterConfig = null; //鍒濆鍖栭厤缃?    protected String encoding = null; 			//鎺ユ敹瀛楃缂栫爜
    protected boolean ignore = true;			//鏄惁蹇界暐澶у皬鍐?	
	/* 鏋愭瀯鍑芥暟
	 * @see javax.servlet.Filter#destroy()
	 */
	@Override
	public void destroy() {
        this.encoding = null;
        this.filterConfig = null;
	}

	/* 鎵ц杩囨護鎿嶄綔
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		
        if (ignore || (request.getCharacterEncoding() == null)) {
            String encoding = selectEncoding(request);	//濡傛灉涓虹┖鍏堜粠web.xml涓緱鍒?            if (encoding != null){
                request.setCharacterEncoding(encoding);	//璁劇疆瀛楃闆嗙紪鐮?			}
        }
        chain.doFilter(request, response);
	}

	/* 鍒濆鍖栬繃婊ゅ櫒
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
    	this.filterConfig = filterConfig;	
        this.encoding = filterConfig.getInitParameter("encoding");	//浠巜eb.xml鏂囦歡涓鍙杄ncoding鐨勫€?        String value = filterConfig.getInitParameter("ignore");		//浠巜eb.xml鏂囦歡涓鍙杋gnore鐨勫€?        //浠ヤ笅涓夌鎯呭喌鍧囦負蹇界暐澶у皬鍐?        if (value == null)
            this.ignore = true;
        else if (value.equalsIgnoreCase("true"))
            this.ignore = true;
        else if (value.equalsIgnoreCase("yes"))
            this.ignore = true;
        else
            this.ignore = false;
	}
	
	/**
	 * 寰楀埌瀛楃缂栫爜
	 * @param request
	 * @return
	 */
    protected String selectEncoding(ServletRequest request) {
        return (this.encoding);
    }
}
           

3.淇敼Tomcat鏈嶅姟鍣╟onf\Server.xml鏂囦歡锛屽姞鍏ヤ竴涓猆RIEncoding="GBK"聽

<!--URIEncoding 閰嶇疆鏈嶅姟鍣ㄥ弽搴旂紪鐮侀泦-->
聽聽聽 <Connector port="8080" protocol="HTTP/1.1" 
聽聽聽聽聽聽聽聽聽聽聽聽聽聽 connectionTimeout="20000" 
聽聽聽聽聽聽聽聽聽聽聽聽聽聽 redirectPort="8443" 
聽聽聽聽聽聽聽聽聽聽聽聽聽聽 URIEncoding="GBK"/>
           

4.璁劇疆鍓嶅彴椤甸潰瀛楃缂栫爜闆?

<%@ page contentType="text/html;charset=GBK" %>
           

//姝ゅ鐨刢harset鐨勫€艱鍜寃eb.xml閲岀殑聽聽 <param-value></param-value>鍊間竴鏍?

=================================瀹?====================================

鍙傝€冭嚜锛? Tomcat鏈嶅姟鍣ㄧ洰褰晈ebapps鐨別xamples绀轟緥

jsp涓枃涔辯爜鐨凢ilter杩囨護鍣ㄩ厤缃細http://www.cnblogs.com/zhuboxingzbx/articles/1208118.html

JSP涓枃涔辯爜锛岀敤杩囨護鍣ㄦ柟寮忚В鍐籌細http://www.zhouwenze.com/archives/20110729-411.html

JSP涓枃涔辯爜杩囨護鍣ㄥ疄渚嬶細http://blog.csdn.net/wkupaochuan/article/details/7461208

========================================================================