编码处理过滤器
CharseSetFilter.java
1 package org.guangsoft.filter;
2
3 import java.io.IOException;
4 import javax.servlet.Filter;
5 import javax.servlet.FilterChain;
6 import javax.servlet.FilterConfig;
7 import javax.servlet.ServletException;
8 import javax.servlet.ServletRequest;
9 import javax.servlet.ServletResponse;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12
13 /**
14 *
15 * @author guanghe
16 */
17 public class CharsetFilter implements Filter
18 {
19
20 @Override
21 public void init(FilterConfig filterConfig) throws ServletException
22 {
23 }
24
25 @Override
26 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
27 {
28 HttpServletRequest request = (HttpServletRequest) req;
29 HttpServletResponse response = (HttpServletResponse) res;
30 request.setCharacterEncoding("utf-8");
31 response.setCharacterEncoding("utf-8");
32 response.setContentType("text/html;charset=utf-8");
33 chain.doFilter(req, res);
34 }
35
36 @Override
37 public void destroy()
38 {
39 }
40
41 }
web.xml
1 <filter>
2 <filter-name>CharsetFilter</filter-name>
3 <filter-class>org.guangsoft.filter.CharsetFilter</filter-class>
4 </filter>
5 <filter-mapping>
6 <filter-name>CharsetFilter</filter-name>
7 <url-pattern>/*</url-pattern>
8 </filter-mapping>