在jsp和servlet的開發中今天自己做的一個小例子中出現了中文亂碼問題用了許多方法發現還是解決不了最後我用了過濾器才算終結
此方法總結如下在jsp頁面中使用<%@ page pageEncoding="GB"%> 這裡的pageEncoding是GB那麼我們在過濾器webxml中所使用的<paramvalue>GB</paramvalue>也為GB總之一條就是上述兩者一致即可比如同為UTF
我將過濾器的java代碼和webxml的配置寫
//java代碼部分CharsetFilter java
package com;
import javaioIOException;
import javaxservletFilter;
import javaxservletFilterChain;
import javaxservletFilterConfig;
import javaxservletServletException;
import javaxservletServletRequest;
import javaxservletServletResponse;
public class CharsetFilter implements Filter {
private String charset = "GB";
public void destroy() {
}
public void doFilter(ServletRequest request ServletResponse response
FilterChain arg) throws IOException ServletException {
requestsetCharacterEncoding(charset);
argdoFilter(request response);
}
public void init(FilterConfig arg) throws ServletException {
charset = arggetInitParameter("charset");
Systemoutprintln("字符過濾器開啟當前狀態"+charset);
}
}
//webxml部分(以下部分可以直接復制粘貼到WEBXML下)
<filter>
<filtername>CharsetFilter</filtername>
<filterclass>comCharsetFilter</filterclass>
<initparam>
<paramname>charset</paramname>
<paramvalue>GB</paramvalue>
</initparam>
</filter>
<filtermapping>
<filtername>CharsetFilter</filtername>
<urlpattern>/*</urlpattern>
</filtermapping>
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20072.html