java ee 第三周 filter
字符过滤器CharacterFilter.java
package
cn.hncu.pubs;
import
java.io.IOException;
import
java.util.Date;
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.annotation.WebFilter;
import
javax.servlet.http.HttpServletResponse;
public
class
CharacterFilter
implements
Filter {
private
String charset;
public
void
destroy() {
}
public
void
doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws
IOException, ServletException {
request.setCharacterEncoding(charset);
response.setCharacterEncoding(charset);
HttpServletResponse resp=(HttpServletResponse) response;
chain.doFilter(request, resp);
}
public
void
init(FilterConfig fConfig)
throws
ServletException {
charset=fConfig.getInitParameter(
"charset"
);
}
}
参考网站:https://www.2cto.com/kf/201608/540067.html