有很多人在开发过程中,会遇到servlet页面传值乱码问题,现在有我来实现一个字符编码过滤器:
代码如下:
public class Filter implements javax.servlet.Filter {
private String adg;public Filter() {
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(this.adg);
response.setCharacterEncoding(this.adg);
chain.doFilter(request, response);
}
public void init(FilterConfig fcg) throws ServletException {
this.adg=fcg.getInitParameter("encoding");
}
web.xml配置文件:
xml配置文件:<filter>
<display-name>Filter</display-name>
<filter-name>Filter</filter-name>
<filter-class>com.test.Filter.Filter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>