1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.tmsp.cms.filter;
/**
 * 字符编码器
 */
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 CharacterEncoding implements Filter {
 
	/**
	 * Constructor of the object.
	 */
	public CharacterEncoding() {
		super();
	}
 
	/**
	 * Initialization of the servlet. <br />
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}
 
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		// TODO Auto-generated method stub
		arg0.setCharacterEncoding("utf-8");
		arg2.doFilter(arg0, arg1);
	}
 
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
 
	}
 
	public void destroy() {
		// TODO Auto-generated method stub
	}
 
}

web.xml 配置

1
2
3
4
5
6
7
<filter>
		</filter><filter -name>CharacterEncoding</filter>
		<filter -class>com.tmsp.cms.filter.CharacterEncoding</filter>
 
	<filter -mapping>
		</filter><filter -name>CharacterEncoding</filter>
		<url -pattern>/*</url>