zoukankan      html  css  js  c++  java
  • 监视器和过滤器的总结

    一、监视器部分

    /**
     * @author lenovo
     * 监听器的使用:
     *    作用:
     *         监听作用域对象request、session、application的创建、销毁和更改
     *    使用:
     *        创建一个实现指定接口的java类
     *            监听request-->ServletRequestListener//监听request对象的创建和销毁
     *                 requestInitialized(ServletRequestEvent sre)//创建
     *                 requestDestroyed(ServletRequestEvent sre)//销毁
     *                 注意:
     *                    形参可以获得request和Servletcontext对象 
     *                    sre.getServletRequest();
     *                    sre.getServletContext();
     *                    
     *                    
     *            监听request数据-->ServletRequestAttributeListener//监听数据的状态     
     *                 attributeAdded(ServletRequestAttributeEvent srae)//增加数据
     *                 attributeRemoved(ServletRequestAttributeEvent srae)//移除数据
     *                 attributeReplaced(ServletRequestAttributeEvent srae)//更改数据
     *                 注意:
     *                    形参可以获得被监听的数据
     *                     srae.getName();//获得键名
    		               srae.getValue();//获得数据
     *                
     *                 
     *            监听session-->HttpSessionListener//监听session的创建和销毁
     *                 sessionCreated(HttpSessionEvent se)//监听session创建
     *                 sessionDestroyed(HttpSessionEvent se)//监听session销毁
     *                 注意:
     *                    形参可以获取session对象
     *                    se.getSession();
     *           监听session数据-->HttpSessionAttributeListener//监听session的数据变化 
     *                 attributeAdded(HttpSessionBindingEvent se)
     *                 attributeRemoved(HttpSessionBindingEvent se)  
     *                 attributeReplaced(HttpSessionBindingEvent se)  
     *                 注意:
     *                  形参可以获取当前被监听的数据
     *                  se.getName();
    		            se.getValue(); 
    		            
    		             
     *          监听application-->ServletContextListener//监听application的初始化和销毁
     *                 contextInitialized(ServletContextEvent sce)
     *                 contextDestroyed(ServletContextEvent sce)
     *                 注意:
     *                   形参可以获取application对象
     *                   sce.getServletContext();
     *          监听application数据--->ServletContextAttributeListener//监听application的数据变更        
     *                   attributeAdded(ServletContextAttributeEvent scab)
     *                   attributeRemoved(ServletContextAttributeEvent scab)
     *                   attributeReplaced(ServletContextAttributeEvent scab)
     *                   注意:
     *                      形参可以获取数据application
     *                      scab.getName();
    		                scab.getValue();
    		                
    		                		                
     *        在web.xml中进行配置监听类
     *               <listener>
                          <listener-class>com.hpu.listener.TestListener</listener-class>
                     </listener>
     *        案例:
     *            统计在线人数
     *            统计网页浏览次数
     */
    

    二、过滤器部分

    /**
     * @author lenovo
     * 
     * 过滤器的使用
     *      作用:
     *         对服务器接收的请求资源和响应给浏览器的资源进行管理
     *         保护servlet
     *      使用:
     *         创建一个实现了Filter的普通Java
     *         覆写接口的方法 
     *               init方法:服务器启动即执行。资源初始化
     *               doFilter方法 :拦截请求的方法,在此方法中实现对资源的管理。
     *                      注意:
     *                         需要手动对请求进行放行。chain.doFilter(request, response);
     *               destory方法 :服务器关闭执行    
     *         在web.xml中配置过滤器
     *              <!--配置过滤器 -->
    				   <filter>
    				      <filter-name>myfilter</filter-name>
    				      <filter-class>com.hpu.filter.MyFilter</filter-class>
    				   </filter>
    				   <filter-mapping>
    				       <filter-name>myfilter</filter-name>
    				       <url-pattern>/*</url-pattern>
    				   </filter-mapping>
    			     注意:
    			     url-pattern:/*
    			                                  表示拦截所有请求
    			     url-pattern:*.do
    			                                  表示拦截所有以do结尾的请求。一般用来进行模块拦截处理
    			      url-pattern:/TestServlet
    			                                  表示拦截指定URL的请求。针对某个servlet请求进行拦截。保护servlet
    			     总结: 
    			                过滤器程序员声明和配置,服务器根据请求中的uri信息调用  
    			     执行:
    			              浏览器发起请求到服务器,服务器接收到请求后,根据URI信息在web.xml找到对应的过滤器,
    			              执行doFilter方法,该方法对此次请求进行处理后如果满足条件进行放行,放行后还有符合要求的过滤器则继续进行过滤
    			              直到找到对应的servlet进行处理  ,servlet对请求处理后,也就是service方法结束后,还会继续返回响应的doFilter进行执行
    			     案例:
    			               同一编码格式处理 
    			     session管理
    			               权限管理 
    			               资源管理(统一水印、词汇和谐)                                                 
     *            
     */
    
  • 相关阅读:
    大话设计模式笔记(十三)の状态模式
    大话设计模式笔记(十二)の抽象工厂模式
    大话设计模式笔记(十一)の观察者模式
    大话设计模式笔记(十)の建造者模式
    大话设计模式笔记(九)の外观模式
    大话设计模式笔记(八)の模板方法模式
    大话设计模式笔记(七)の原型模式
    Vue(十二):自定义指令和函数渲染
    Vue(十一):组件边界
    Vue(十):混入、插件和过滤器
  • 原文地址:https://www.cnblogs.com/tuboshu/p/10752277.html
Copyright © 2011-2022 走看看