栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

JavaWeb(过滤器&监听器)

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaWeb(过滤器&监听器)

1.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("----过滤器被销毁----");
}

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执行之后----");
}

public void init(FilterConfig fConfig) throws ServletException {
	System.out.println("----过滤器初始化----");
}

(2)在web.xml文件中配置访问过滤路径(和Servlet的配置有些相似)


  	FilterDemo
  	com.zking.filter.FilterDemo
  
  
  	FilterDemo
  	
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:


	EncodingFilter
  	com.zking.filter.EncodingFilter
  	
  	
  		encoding
  		utf-8
  	


	EncodingFilter
  	/*

2.Listener监听器

1.概述

  • 监听器就是一个专门用于:对其它对象产生的特定事件,或状态改变后进行监听和相应处理的对象
  • 监听器其实就是一个实现特定接口的普通 Java程序,这个程序专门用于监听另一个 Java对象的方法调用或属性改变。
  • 当被监听对象发生上述事件后,监听器某个方法立即被执行。
  1. 使用场景:
    (1)统计在线人数和在线用户
    (2)系统启动时加载初始化信息
    (3)统计网站访问量
    (4)跟Spring结合,做相关操作

  2. 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:

 

   com.listener.Application

案例统计在线人数和在线用户:

① 编写监听事件

// 首先我们需要实现 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


    
    com.molu.listener.OnlineCountListener

③ 布局模拟


当前网站在线人数为: ${applicationScope.get("onlineCount")}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/882682.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号