package 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 Encodingfilter implements Filter { //定义属性保存字符编码集 String encoding; //构造函数 public Encodingfilter() { System.out.println("过滤器构造函数!"); } //销毁的方法 public void destroy() { System.out.println("过滤器销毁!"); } //过滤的方法 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("过滤器开始过滤!"); //中文乱码处理 response.setContentType("text/html;charset="+encoding); response.setCharacterEncoding(encoding); request.setCharacterEncoding(encoding); //chain对象就是过滤器链对象 //chain对象的doFilter方法令请求往下一个过滤器传递 chain.doFilter(request, response); } //初始化的方法 public void init(FilterConfig fConfig) throws ServletException { System.out.println("过滤器初始化!"); //从配置文件中读取字符编码集 encoding = fConfig.getInitParameter("encoding"); } }
package 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 Encodingfilter implements Filter { //定义属性保存字符编码集 String encoding; //构造函数 public Encodingfilter() { System.out.println("过滤器构造函数!"); } //销毁的方法 public void destroy() { System.out.println("过滤器销毁!"); } //过滤的方法 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("过滤器开始过滤!"); //中文乱码处理 response.setContentType("text/html;charset="+encoding); response.setCharacterEncoding(encoding); request.setCharacterEncoding(encoding); //chain对象就是过滤器链对象 //chain对象的doFilter方法令请求往下一个过滤器传递 chain.doFilter(request, response); } //初始化的方法 public void init(FilterConfig fConfig) throws ServletException { System.out.println("过滤器初始化!"); //从配置文件中读取字符编码集 encoding = fConfig.getInitParameter("encoding"); } }
xml配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>ch03</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list>
<context-param> <param-name>userName</param-name> <param-value>sa</param-value> </context-param> <context-param> <param-name>pwd</param-name> <param-value>888</param-value> </context-param> <!--过滤器--> <filter> <display-name>Encodingfilter</display-name> <filter-name>Encodingfilter</filter-name> <filter-class>filter.Encodingfilter</filter-class> <!--初始化--> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>Encodingfilter</filter-name> <url-pattern>/*</url-pattern> <!--/*代表所有--> </filter-mapping> </web-app>