在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>