servlet过滤器
认识过滤器
•Servlet过滤器本身并不生成请求和响应对象,它只提供过滤作用。
•Servlet过滤器能够在Servlet被调用之前检查Request对象,修改Request Header和Request内容;
•在Servlet被调用之后检查Response对象,修改Response Header和Response内容。Servlet过滤器负责过滤的Web组件可以是Servlet、JSP或HTML文件。
•过滤器的工作流程
filter接口
•init(FilterConfig):这是Servlet过滤器的初始化方法,Servlet容器创建Servlet过滤器实例后将调用这个方
法。在这个方法中可以读取web.xml 文件中Servlet过滤器的初始化参数
实现:
this.config = config; blacklist=config.getInitParameter("blacklist");
•doFilter(ServletRequest, ServletResponse,FilterChain):
这个方法完成实际的过滤操作。当客户请求访问与过滤器关联的URL时,Servlet容器将先调用过滤器的doFilter方法。FilterChain参数用于访问后续过滤器。
实现:
HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse resp = (HttpServletResponse) response; //do something if(...){ chain.doFilter(req, resp); }else{ req.getRequestDispatcher("/someone.jsp").forward(req,resp); }
•destroy():Servlet容器在销毁过滤器实例前调用该方法,在这个方法中可以释放Servlet过滤器占用的资源
串联的filter
在web.xml中配置filter
提示:在web.xml文件中,必须先配置Servlet过滤器,再配置Servlet。
url-pattern匹配规则
①完全匹配:以“/”开头,以字母(非“*”)结束
如:<url-pattern>/test/list.do</url-pattern>
②目录匹配:以“/”开头且以“/*”结尾
如:<url-pattern>/test/*</url-pattern>
<url-pattern>/*</url-pattern>
③扩展名匹配:以“*.”开头,以扩展名结束
如:<url-pattern>*.do</url-pattern>
④ “/” 用来表明对应的Servlet为应用默认的Servlet。在这种情况下Servlet路径是请求的URI去掉上下文路径并且路径信息为null。
如果我们用filter想过滤某个servlet,其<url-pattern>标签中的内容与servlet中<url-pattern>标签中内容一样。也就是完全匹配。或者在<servlet-name>标签中写servlet的name。
一个filter对应一个url-pattern
<filter> <filter-name>authority</filter-name> <filter-class>com.util.AuthorityFilter</filter-class> <init-param> <param-name>blacklist</param-name> <param-value>someone</param-value> </init-param> </filter> <filter-mapping> <filter-name>authority</filter-name> <url-pattern>/pages1/*</url-pattern> </filter-mapping>
一个filter对应多个url-pattern
<filter> <filter-name>authority</filter-name> <filter-class>com.util.AuthorityFilter</filter-class> </filter> <filter-mapping> <filter-name>authority</filter-name> <url-pattern>/pages1/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>authority</filter-name> <url-pattern>/pages2/*</url-pattern> </filter-mapping>
配置串联的filter
<filter> <filter-name>Filter1</filter-name> <filter-class>Filter1</filter-class> </filter> <filter> <filter-name>Filter2</filter-name> <filter-class>Filter2</filter-class> </filter> <filter-mapping> <filter-name>Filter1</filter-name> <url-pattern>/note</url-pattern> </filter-mapping> <filter-mapping> <filter-name>Filter2</filter-name> <url-pattern>/note</url-pattern> </filter-mapping>
编码过滤器
EncodingFilter.java
package com.kl.filter; import java.io.IOException; import javax.servlet.*; public class EncodingFilter implements Filter { private FilterConfig config=null; private String encoding=""; @Override public void init(FilterConfig filterConfig) throws ServletException { this.config=filterConfig;//得到初始化参数 this.encoding=filterConfig.getInitParameter("encoding"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req=(HttpServletRequest)request; HttpServletResponse resp=(HttpServletResponse)response; req.setCharacterEncoding(encoding);//改变post方式请求参数的编码 resp.setCharacterEncoding(encoding);//改变响应的编码 System.out.println("encoding:"+encoding); chain.doFilter(req, resp); } @Override public void destroy() { } }
web.xml中的配置
<filter> <filter-name>encodingFilter</filter-name> <filter-class>com.kl.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> <filter>
一点说明
此过滤器只能过滤表单post方法的参数编码,如果表单提交用的是get方法,请用下面方式转码
String string = request.getParamers(""); String = new String(string.getBytes("ISO8859-1","utf-8"));