日期:2014-05-20  浏览次数:20854 次

关于字符编码过滤器的问题
package com.csdn.tool;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

public class CharacterEncodingFilter implements Filter {
private FilterConfig config;
private String encoding = "UTF-8";

public void destroy() {
config = null;
}
@Override
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOExceptionServletException {
request.setCharacterEncoding(encoding);
chain.doFilter(request, response);
}

public void init(FilterConfig config) throws ServletException {
this.config = config;
String s = config.getInitParameter("encoding");
if (s != null) {
encoding = s;
}
}
}


这段代码仅提示
@Override
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
chain.doFilter(request, response);
}

这个方法没能正确重载Filter的方法doFilter() 去掉@Override符号就好了  但是我查看了Servlet API文档

Filter接口确实有这个方法 求大牛给出解决办法
编码 Filter

------解决方案--------------------
继承了人家的接口却不实现?
没看懂你意思
转码:
public void doFilter(ServletRequest arg0, ServletResponse arg1, 
FilterChain arg2) throws IOException, ServletException { 
//参数转换,因为我们已经它肯定是Http协议的请求。 
HttpServletRequest request = (HttpServletRequest)arg0; 
HttpServletResponse response = (HttpServletResponse)arg1; 
//设置request和response使用的编码均为UTF-8。 
request.setCharacterEncoding("UTF-8"); 
response.setCharacterEncoding("UTF-8"); 
response.setContentType("text/html;charset=UTF-8");