为什么不过滤啊?大侠们帮我看看吧。谢谢!
package encoding;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class encodingFilter implements Filter {
private FilterConfig config;
private String Encoding="iso8859-1";
public void destroy() {
config=null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(Encoding);
chain.doFilter(request, response);
}
public void init(FilterConfig con) throws ServletException {
this.config=con;
String s=config.getInitParameter("Encoding");
if(s!=null)
{
this.Encoding=s;
System.out.println(Encoding);
}
}
}
XML
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>encoding.encodingFilter</filter-class>
<init-param>
<param-name>Encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
------解决方案--------------------------------------------------------
在request.setCharacterEncoding(Encoding);
的下面加上response.setCharacterEncoding(Encoding);试试看