Posted by:forx86
Posted on:2006-08-06 11:09
用的tomcat 5.5,
论坛中提供的这个实现filter用的java文件是否要事先编译为class文件?若不用,这个java文件放到哪呢?
“对于post方式建议用配置过滤器的方式来解决,因为这样,配置一个地方整个系统都不用操心了。
简单说明:
web.xml
Set Character Encoding
SetCharacterEncodingFilter
Set Character Encoding
/*
/************************/
SetCharacterEncodingFilter.java
--------------------------------------------
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;
import javax.servlet.UnavailableException;
/**
* Example filter that sets the character encoding to be used in parsing the
* incoming request
*/
public class SetCharacterEncodingFilter implements Filter {
/**
* Take this filter out of service.
*/
public void destroy() {
}
/**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)throws IOException, ServletException {
request.setCharacterEncoding("GBK");
// 传递控制到下一个过滤器
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
}
}
也可以把编码做为参数传递进去。
”