一、Filter过滤器
1. 概述
过滤器是JavaWeb三大组件之一(servlet、Listener、Filter),过滤器实际上就是对web资源(HTML、CSS、Servlet、JSP)进行拦截,做一些处理后再交给下一个过滤器或servlet处理,通常都是用来拦截request进行处理的,也可以对返回的response进行拦截处理
2. 过滤器使用场景
① 自动登录
② 统一设置编码格式
③ 访问权限控制
④ 敏感字符过滤等
3.Filter快速入门
(1)编写一个Filter,定义类实现接口Filter,实现接口中的抽象方法接口:
public class FilterDemo1 implements Filter{}
方法:
/** * 过滤器对象被销毁之前执行的方法 * 服务器关闭或重载时,过滤器对象销毁 */ public void destroy() { System.out.println("----过滤器被销毁----"); } /** * 对请求进行过滤的方法 * ServletRequest 请求对象 * ServletResponse 响应对象 * FilterChain 过滤链对象 * 作用:将通过此过滤器的请求,传递给下一个过滤器或目标Servlet */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("----过滤器执行过滤方法----"); System.out.println("----DemoFilter-doFilter执行之前----"); // 对请求放行的方法 chain.doFilter(request, response); System.out.println("----DemoFilter-doFilter执行之后----"); } /** * 过滤器对象创建之后执行的方法,对过滤器进行初始化 * 其中FilterConfig参数为当前过滤器的配置,获取过滤器的初始化参数 * 当服务器启动时,过滤器对象被创建 */ public void init(FilterConfig fConfig) throws ServletException { System.out.println("----过滤器初始化----"); }
(2)在web.xml文件中配置访问过滤路径(和Servlet的配置有些相似)<filter> <filter-name>FilterDemo</filter-name> <filter-class>com.zking.filter.FilterDemo</filter-class> </filter> <filter-mapping> <filter-name>FilterDemo</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
4.常用配置项 urlPatterns
① 以指定资源匹配。例如"/index.jsp"
② 以目录匹配。例如"/servlet/*"
③ 以后缀名匹配,例如"*.jsp"
④ 通配符,拦截所有web资源。"/*"过滤路径
1.具体路径:某个Servlet的路径
例如:/hello
2.路径后缀统配:*.后缀名
例如: *.do *.html *.action
3.全路径统配: /*
过滤所有的请求,包括请求Servlet,jsp,以及请求静态资源
5.Filter生命周期
(1)构造器:Filter的构造器在服务器启动时调用。 构造器只会调用一次,说明Filter也是单例多线程的。
(2)init():在构造器被调用后,紧接着被调用。作用:用来初始化Filter。
(3)doFilter():每一次拦截请求时都会调用。
(4)destroy 方法在项目停止时调用,用来在对象被销毁前做一些收尾工作。
6. 多个Filter的执行顺序
(1)在web.xml中,filter执行顺序跟<filter-mapping>的顺序有关,先声明的先执行
(2)使用注解配置的话,filter的执行顺序跟名称的字母顺序有关,例如AFilter会比BFilter先执行
(3)如果既有在web.xml中声明的Filter,也有通过注解配置的Filter,那么会优先执行web.xml中配置的Filter7. 过滤器在WEB应用中实例
实现接口重写方法:
package com.lixiangning.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 { private String encoding; @Override public void destroy() { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("------字符集过滤器开始过滤------"); // 对请求对象设置字符集 servletRequest.setCharacterEncoding(encoding); // 对相应对象设置字符集 servletResponse.setCharacterEncoding(encoding); servletResponse.setContentType("text/html;charset=" + encoding); // 放行 filterChain.doFilter(servletRequest, servletResponse); } @Override public void init(FilterConfig filterConfig) throws ServletException { String encoding = filterConfig.getInitParameter("encoding"); this.encoding = encoding; } }
配置xml:
<filter> <filter-name>EncodingFilter</filter-name> <filter-class>com.zking.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>
二、 Listener监听器
1.概述
1.1 监听器就是一个专门用于:对其它对象产生的特定事件,或状态改变后进行监听和相应处理的对象
1.2 监听器其实就是一个实现特定接口的普通 Java程序,这个程序专门用于监听另一个 Java对象的方法调用或属性改变。
1.3 当被监听对象发生上述事件后,监听器某个方法立即被执行。2. 使用场景:
(1)统计在线人数和在线用户
(2)系统启动时加载初始化信息
(3)统计网站访问量
(4)跟Spring结合,做相关操作3. 3种常用监听器
(1)application监听器 servlet---ServletContext实现:ServletContextListener (常用)
重写方法://容器启动时调用 public void contextInitialized(ServletContextEvent event){ } //容器消毁时调用 public void contextDestroyed(ServletContextEvent event){ }
ServletContextEvent事件方法:
event.getServletContext().getContextPath();//取得ServletContext对象,即上下文
(2)Session监听器
实现:HttpSessionListener (偶尔用)
重写://session创建时调用 public void sessionCreated(HttpSessionEvent event){ } //session销毁时调用 public void sessionDestroyed(HttpSessionEvent event){ }
HttpSessionEvent事件方法:
event.getSession().getId(); //得到session的ID
实现:HttpSessionAttributeListener (不用,性能差)
重写://增加属性时触发 public void attributeAdded(HttpSessionBindingEvent event){ } //删除属性时触发 public void attributeRemoved(HttpSessionBindingEvent event){ } //替换属性时触发 public void attributeReplaced(HttpSessionBindingEvent event){ }
HttpSessionBindingEvent事件方法:
event.getSession() //取得session
event.getName() //取得属性的名称
event.getValue() //取得属性的内容
(3)request监听器
实现:ServletRequestListener (不用,性能差)
重写://请求开始时调用 public requestInitialized(ServletRequestEvent event){ } //请求结束时调用 public requestDestroyed(ServletRequestEvent event){ }
ServletRequestEvent事件方法:
event.getServletRequest().getRemoteAddr(); //得到IP地址
event.getServletContext().getContextPath(); //得到当前路径配置xml:
<listener> <listener-class>com.listener.Application</listener-class> </listener>
案例统计在线人数和在线用户:
① 编写监听事件
// 首先我们需要实现 Servlet规定的监听器接口 public class OnlineCountListener implements HttpSessionListener { // 实现该接口后会必须重写下面这两个方法 @Override public void sessionCreated(HttpSessionEvent se) { // 该方法是会在 Session创建时被调用,也就是 Session创建的监听事件 // 拿到上下文对象 ServletContext context = se.getSession().getServletContext(); Integer onlineCount = (Integer) context.getAttribute("onlineCount"); // 在触发 Session创建监听事件时,如果 onlineCount变量为 0我们将其设置为 1,表示第一个用户在线 if (onlineCount==null){ onlineCount = new Integer(1); // 如果不为 0表示之前有用户在线,我们将在线人数 +1 }else { int count = onlineCount.intValue(); onlineCount = new Integer(count+1); } // 打印输出 方便测试,可以去掉 System.out.println(onlineCount); // 将在线人数的变量赋值添加到上下文对象中,方便前端取值 context.setAttribute("onlineCount",onlineCount); } @Override public void sessionDestroyed(HttpSessionEvent se) { // 这个方法则相反,会在Session被销毁时调用 // 销毁部分则逻辑相反 ServletContext context = se.getSession().getServletContext(); Integer onlineCount = (Integer) context.getAttribute("onlineCount"); if (onlineCount==null){ onlineCount = new Integer(0); }else { int count = onlineCount.intValue(); onlineCount = new Integer(count-1); } context.setAttribute("onlineCount",onlineCount); } }
② 配置xml
<listener> <!--过滤器的注册相对比较简单--> <listener-class>com.molu.listener.OnlineCountListener</listener-class> </listener>
③ 布局模拟
<body> <div> <h2>当前网站在线人数为: <span style="background-color: aquamarine"> ${applicationScope.get("onlineCount")} </span> </h2> </div> </body>