标签:
本文源码——GitHub:
https://github.com/imperio-wxm/wordpressCode/tree/master/Filter
【没有过滤器】:
用户直接访问WEB资源。
【有过滤器】:
WEB容器启动时进行过滤器的加载,用户发出请求到过滤器,过滤器判断请求是否符合规则,符合规则的请求通过过滤器发送给WEB资源,WEB资源响应的信息返回给过滤器,过滤器再将WEB资源的响应返回给用户。
1.实例化——通过web.xml进行配置加载,在容器启动时只会实例化一次
2.初始化——调用init()方法,加载信息只会执行一次
3.过滤——使用doFilter()方法进行多次过滤
4.销毁——WEB容器关闭时调用destroy()方法进行销毁
所有的Servlet过滤器都必须实现javax.servlet.Filter接口,并实现该接口中的三个方法。
【生命周期代码演示】
package com.filter; import javax.servlet.*; import java.io.IOException; /** * Created by wxm-Imperio */ public class FirstFilter implements Filter { @Override public void destroy() { System.out.println("destroy,firstFilter"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException { System.out.println("start,doFilter,firstFilter"); filterChain.doFilter(servletRequest, servletResponse); System.out.println("End,doFilter,firstFilter"); } @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("init。firstFilter"); } }
【web.xml配置】
<filter> <filter-name>Filter的名字</filter-name> <filter-class>Filter类的名字</filter-class> <init-param> <description>描述信息可以省略或者放在此处</description> <param-name>参数名称</param-name> <param-value>参数的值</param-value> </init-param> </filter> <filter-mapping> <filter-name>Filter的名字</filter-name> <url-pattern>URL</url-pattern> <dispatcher></dispatcher>//过滤器类型 </filter-mapping>
注意:一般Filter配置在所有的Servlet之前。
【过滤器链的web.xml配置】
<!--过滤器1的配置信息--!> <filter> <filter-name>Filter1的名字</filter-name> <filter-class>Filter1类的名字</filter-class> <init-param> <description>描述信息可以省略或者放在此处</description> <param-name>参数名称</param-name> <param-value>参数的值</param-value> </init-param> </filter> <filter-mapping> <filter-name>Filter1的名字</filter-name> <url-pattern>URL</url-pattern> <dispatcher></dispatcher>//过滤器类型 </filter-mapping> <!--过滤器2的配置信息--!> <filter> <filter-name>Filter2的名字</filter-name> <filter-class>Filter2类的名字</filter-class> <init-param> <description>描述信息可以省略或者放在此处</description> <param-name>参数名称</param-name> <param-value>参数的值</param-value> </init-param> </filter> <filter-mapping> <filter-name>Filter2的名字</filter-name> <url-pattern>URL</url-pattern> <dispatcher></dispatcher>//过滤器类型 </filter-mapping>
注意:过滤器的执行顺序就是在web.xml里配置的顺序。
Servlet2.5:
@WebFilter(servletNames = {"SimpleServlet"} filterName = "SimpleFilter") public class LessThanSixFilter implements Filter { //类中内容 }
【ERROR的配置信息】
<error-page> <error-code>404</error-code> <location>/error.jsp</location> </error-page> <filter> <filter-name>errorFilter</filter-name> <filter-class>com.filter.ErrorFilter</filter-class> </filter> <filter-mapping> <filter-name>errorFilter</filter-name> <url-pattern>/error.jsp</url-pattern> </filter-mapping>
【ErrorFilter过滤器代码】
public class ErrorFilter implements Filter { @Override public void destroy() { } @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException { System.out.println("检测到有错误信息"); } }
Servlet3.0:
【异步操作处理代码:过滤器不等待线程,直接执行后面内容,实现异步处理】
AsynServlet
//设置Servlet支持异步 @WebServlet(name = "AsynServlet", asyncSupported = true) public class AsynServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { System.out.println("Servlet执行开始时间" + new Date()); //实现异步操作 AsyncContext context = request.startAsync(); //开启异步线程 new Thread(new Executor(context)).start(); System.out.println("Servlet执行结束时间" + new Date()); } protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request, response); } //内部类模拟线程 public class Executor implements Runnable { private AsyncContext context; //实现构造方法 public Executor(AsyncContext context) { this.context = context; } @Override public void run() { //执行相关的复杂业务 try { Thread.sleep(1000 * 10); //context.getRequest(); //context.getResponse(); System.out.println("业务执行完成时间" + new Date()); } catch (InterruptedException e) { e.printStackTrace(); } } } }
AsvnFilter过滤器
//注解 @WebFilter(filterName =
"AsynFilter", value = {"/AsynServlet"}, asyncSupported = true, dispatcherTypes = {DispatcherType.REQUEST,DispatcherType.ASYNC}) public class AsynFilter implements Filter { @Override public void destroy() { } @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException { System.out.println("Start..........AsynFilter"); filterChain.doFilter(servletRequest, servletResponse); System.out.println("End..........AsynFilter"); } }
web.xml配置
<servlet> <servlet-name>AsynServlet</servlet-name> <servlet-class>com.servlet.AsynServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AsynServlet</servlet-name> <url-pattern>/AsynServlet</url-pattern> </servlet-mapping>
Filter的init方法中提供了一个FilterConfig对象,提供相关的操作:
如获取Filter中配置的初始化参数web.xml配置:
<filter> <filter-name>LoginFilter</filter-name> <filter-class>com.itzhai.login.LoginFilter</filter-class> <init-param> <param-name>username</param-name> <param-value>arthinking</param-value> </init-param> </filter>
在init()方法中获取:
@Override public void init(FilterConfig filterConfig) throws ServletException { //获取Filter初始化参数 String username = filterConfig.getInitParameter("username"); }
在Filter中访问application:
ServletContext context = filterConfig.getServletContext();
也可以在doFilter方法中根据转换好的request获取:
HttpServletRequest req = (HttpServletRequest)request; ServletContext context = req.getSession().getServletContext();
慕课网中用户登录的加强版Demo,利用过滤器对用于请求和服务器回应进行过滤。
标签:
原文地址:http://www.cnblogs.com/AutumnRhyme/p/4316280.html