java 代码
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- request.setCharacterEncoding("UTF-8");
- chain.doFilter(request, response);
- }
统一字符编码的过滤器要这么写,就把过滤的东西固定在类中了,要换的时候得从新的编译,因此一般的方法是写到web.xml中
而这里只是一个变量名.
比如:
request.setCharacterEncoding("encoding");
在web.xml中通过为encoding这个变量赋个值.
所以,原理就是这个,
通过初始化先读取配置文件的encoding,然后在把它赋值给这里就ok了
所以,过滤器的写法很固定了.只要有个标准的模板改下就可以了.