- 定义
- <pre class="html" name="code"><filter>
- <filter-name>encodingfilter</filter-name>
- <filter-class>com.my.app.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></pre><br>
- <pre></pre>
- <pre></pre>
- 作用
用于指定WEB容器的过滤器,在请求和响应对象在Servlet处理之前和之后,可以通过此过滤器对这两个对象进行处理。
- 备注
filter-class 中指定的过滤器类须继承 javax.servlet.Filter 具有须有以下三种方法
init(FilterConfig filterConfig):初始化;一般情况下时读取配置文件中的init-param参数值 如 filterConfig.getInitParameter("encoding")
doFilter(...):用于对request,response进行处理,并能过chain.doFilter(...) 交过下一个控制器
destroy():资源销毁
- 示例:如编码过滤器
web.xml配置
- <filter>
- <filter-name>encodingfilter</filter-name>
- <filter-class>com.my.app.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>
Java 代码
- /**
- *
- */
- package com.myapp;
- 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;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- * @author louisliao
- *
- */
- public class EncodingFilter implements Filter {
- /**
- * 配置中默认的字符编码
- */
- protected String encoding = null;
- protected FilterConfig filterConfig;
- /**
- * 当没有指定默认编码时是否允许跳过过滤
- */
- protected boolean ignore = true;
- /**
- *
- */
- public EncodingFilter() {
- // TODO Auto-generated constructor stub
- }
- /* (non-Javadoc)
- * @see javax.servlet.Filter#destroy()
- */
- public void destroy() {
- // TODO Auto-generated method stub
- this.encoding=null;
- this.filterConfig=null;
- }
- /* (non-Javadoc)
- * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
- */
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- // TODO Auto-generated method stub
- HttpServletRequest hRequest=(HttpServletRequest)request;
- HttpServletResponse hResponse=(HttpServletResponse)response;
- //Conditionally select and set the character encoding to be used
- if(ignore || hRequest.getCharacterEncoding()==null){
- String coding=selectEncoding(hRequest);
- if(coding!=null){
- hRequest.setCharacterEncoding(coding);
- hResponse.setCharacterEncoding(coding);
- }
- }
- //将控制器传向下一个filter
- chain.doFilter(hRequest, hResponse);
- }
- /* (non-Javadoc)
- * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
- */
- public void init(FilterConfig filterConfig) throws ServletException {
- // TODO Auto-generated method stub
- this.filterConfig=filterConfig;
- this.encoding=filterConfig.getInitParameter("encoding");
- System.out.println(this.encoding);
- String value = filterConfig.getInitParameter("ignore");
- if (value == null) {
- this.ignore = true;
- } else if (value.equalsIgnoreCase("true")) {
- this.ignore = true;
- } else if (value.equalsIgnoreCase("yes")) {
- this.ignore = true;
- } else {
- this.ignore = false;
- }
- }
- protected String selectEncoding(ServletRequest request) {
- return (this.encoding);
- }
- }
- init方法是在WEB应用启动就会调用,doFilter则是在访问filter-mapping映射到的url时会调用。