过滤器的好处:使用了过滤器,无论是Get/Post方式,接收参数时,都不需要再额外的进行转码,过滤器会自动的帮我们办这件事,乱码问题很好的解决了。
方式一:配置文件方式配置过滤器
- 创建CharacterEncodingFilter类,实现Filter接口
public class CharacterEncodingFilter implements Filter {
private String encode = "UTF-8";
@Override
public void init(FilterConfig arg0) throws ServletException {
String encoding = arg0.getInitParameter("encode");
if (encoding != null) {
encode = encoding;
}
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;
// post方式设置编码格式:
request.setCharacterEncoding(encode);
response.setContentType("text/html;charset=" + encode);
arg2.doFilter(new CharacterEncodingRequest(request), arg1);
}
@Override
public void destroy() {
}
}
/**
* 对Get方式传递的请求参数进行编码
*/
class CharacterEncodingRequest extends HttpServletRequestWrapper {
private HttpServletRequest request;
public CharacterEncodingRequest(HttpServletRequest request) {
super(request);
this.request = request;
}
/**
* 对参数重新编码
*/
@Override
public String getParameter(String name) {
String value = super.getParameter(name);
if (value == null) {
return null;
}
String method = request.getMethod();
if ("Get".equalsIgnoreCase(method)) {
try {
value = new String(value.getBytes("ISO8859-1"),
request.getCharacterEncoding());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return value;
}
}
- 在web.xml中配置过滤器
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.filter.util.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encode</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
方式二:注解的方式配置过滤器。这种方式较为简单。对过滤器的配置,以注解的方式定义。
@WebFilter(initParams = { @WebInitParam(name = "encode", value = "UTF-8") }, urlPatterns = { "/*" })
public class CharacterFilter implements Filter {
private String encoding = "UTF-8";
@Override
public void init(FilterConfig arg0) throws ServletException {
String encode = arg0.getInitParameter("encode");
if (encode != null) {
encoding = encode;
}
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;
request.setCharacterEncoding(encoding);
response.setContentType("text/html;charset=" + encoding);
arg2.doFilter(new CharacterEncodingRequest(request), arg1);
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
}
/**
* 对Get方式传递的请求参数进行编码
*/
class CharacterEncodingRequest extends HttpServletRequestWrapper {
private HttpServletRequest request;
public CharacterEncodingRequest(HttpServletRequest request) {
super(request);
this.request = request;
}
/**
* 对参数重新编码
*/
@Override
public String getParameter(String name) {
String value = super.getParameter(name);
if (value == null) {
return null;
}
String method = request.getMethod();
if ("Get".equalsIgnoreCase(method)) {
try {
value = new String(value.getBytes("ISO8859-1"),
request.getCharacterEncoding());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return value;
}
}
欢迎大家批评指正。