【用法】
1、建立配置文件:
1 package com.bjpowernode.drp.util.filter; 2 3 import java.io.IOException; 4 5 import javax.servlet.Filter; 6 import javax.servlet.FilterChain; 7 import javax.servlet.FilterConfig; 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletRequest; 10 import javax.servlet.ServletResponse; 11 12 /** 13 * 采用Filter统一处理字符集 14 * @author yin 15 * 16 */ 17 public class CharsetEncodingFilter implements Filter { 18 19 @Override 20 public void destroy() { 21 22 } 23 24 @Override 25 public void doFilter(ServletRequest request, ServletResponse response, 26 FilterChain chain) throws IOException, ServletException { 27 28 //设置字符集 29 request.setCharacterEncoding("GB18030"); 30 //继续执行 31 chain.doFilter(request, response); 32 } 33 34 @Override 35 public void init(FilterConfig arg0) throws ServletException { 36 37 38 } 39 40 }
2、配置web.XML配置文件
1 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 4 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 5 6 <!-- 文件名和文件相对路径 --> 7 <filter> 8 <filter-name>CharsetEncodingFilter</filter-name> 9 <filter-class>com.bjpowernode.drp.util.filter.CharsetEncodingFilter</filter-class> 10 </filter> 11 12 <!-- 文件名和拦截的页面 --> 13 <filter-mapping> 14 <filter-name>CharsetEncodingFilter</filter-name> 15 <url-pattern>*.jsp</url-pattern> 16 </filter-mapping> 17 18 </web-app>
【改进】-字符集通过读取XML文件取得
web.XML文件:
1 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 4 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 5 6 <!-- 文件名和文件相对路径 --> 7 <filter> 8 <filter-name>CharsetEncodingFilter</filter-name> 9 <filter-class>com.bjpowernode.drp.util.filter.CharsetEncodingFilter</filter-class> 10 <init-param> 11 <param-name>encoding</param-name> 12 <param-value>GBK</param-value> 13 </init-param> 14 </filter> 15 16 <!-- 文件名和拦截的页面 --> 17 <filter-mapping> 18 <filter-name>CharsetEncodingFilter</filter-name> 19 <url-pattern>*.jsp</url-pattern> 20 </filter-mapping> 21 22 </web-app>
Filter 文件
1 package com.bjpowernode.drp.util.filter; 2 3 import java.io.IOException; 4 5 import javax.servlet.Filter; 6 import javax.servlet.FilterChain; 7 import javax.servlet.FilterConfig; 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletRequest; 10 import javax.servlet.ServletResponse; 11 12 /** 13 * 采用Filter统一处理字符集 14 * @author yin 15 * 16 */ 17 public class CharsetEncodingFilter implements Filter { 18 19 private String endcoding; 20 @Override 21 public void destroy() { 22 23 } 24 25 @Override 26 public void doFilter(ServletRequest request, ServletResponse response, 27 FilterChain chain) throws IOException, ServletException { 28 29 //设置字符集 30 request.setCharacterEncoding(endcoding); 31 //继续执行 32 chain.doFilter(request, response); 33 } 34 35 //初始化方法 36 @Override 37 public void init(FilterConfig filterConfig) throws ServletException { 38 //读取web.xml文件中 字符集的值 39 this.endcoding = filterConfig.getInitParameter("encoding"); 40 System.out.println(endcoding); 41 42 } 43 44 }
request.getParameter() 和request.getAttribute() 区别
JavaWeb过滤器Filter
javaweb学习总结(四十二)——Filter(过滤器)学习
总结:不断提高自己的编码效率!
时间: 2024-10-18 10:52:03