JSP基础:(4)过滤器与监听器

过滤器

      Filter根据字面上的意思就是过滤器。
            它具有以下特点: 
            (1)声明式的:通过配置来决定是否启用
            (2)模块化的:是一个普通的Java类
            (3)可移植的:稍加更改,便能实现重用
            (4)透明的:对客户端而言,就好像不存在一样。

    实现接口中的三个方法:

            init(FilterConfig filterConfig) throws ServletException 

                   当servlet容器创建该filter实例的时候调用该方法进行初始化设置。

            doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws java.io.IOException, ServletException 

                   当客户端请求服务资源或是回复的时候调用该方法进行filter功能处理。

            destroy( ) 

                当服务不在使用该filter功能的时候被web容器调用,此后的web请求将不在进行doFilter中的处理。         

     Filter-示例:           

              public class Encoder implements Filter {
                public void init(FilterConfig arg0) throws ServletException {
                    System.out.println("过滤器初始化");
                }
                public void doFilter(ServletRequest arg0, ServletResponse arg1,
                    FilterChain arg2) throws IOException, ServletException {
                //在执行过滤时,测试输出。
                    System.out.println("测试执行。。");    
                    arg2.doFilter(req,res);//进入下一过滤链或Servlet
                }
                public void destroy() {
                    System.out.println("过滤器被销毁");
                }
            } 

            web.xml文件中的配置               

                <filter>
                    <filter-name>code</filter-name>
                    <filter-class>tools.Encoder</filter-class>
                </filter>
                <filter-mapping>
                    <filter-name>code</filter-name>
                    <url-pattern>/*</url-pattern>
                </filter-mapping>

        过滤器的生命周期 

            和Servlet一样,过滤器的生命周期分为3个阶段。
            1、启动服务器时加载过滤器的实例,并调用init()方法来初始化实例; 
            2、每一次请求时都只调用方法doFilter()进行处理; 
            3、停止服务器时调用destroy()方法,销毁实例

Web监听器

        Servlet监听器是在Servlet2.4规范中引入的一项重要功能。给Web应用增加事件处理机制,以便更好的监视和控制Web应用状态的变化。可以监听客户端的请求、服务端状态的操作等 

        监听器分类:

                        ServletContext监听web上下文信息
                        HttpSession监听Servelt的会话信息
                       ServletRequest监听Servlet的请求信息

            在Servlet2.4中提供了三种对象的事件监听:

             1.ServletContext:监听web上下文信息

                    1.1ServletContextListener接口监听下文对象状态                       

                       public interface ServletContextListener{
                            public void contextInitialized(ServletContextEvent sc);
                                public void contextDestroyed(ServletContextEvent sc);
                        }

                        方法及参数说明:
                            contextInitialized():当容器加载Web应用时,调用此方法。
                            contextDestroyed():当容器销毁或移出Web应用时,调用此方法。
                            ServletContentEvent是一个ServletContext对象的事件类,其中包含方法:
                                    ServletContext getServletContext()方法:  该方法返回事件的Servlet上下文,即产生事件的当前应用程序
                    1.2ServletContextAttributeListener接口监听上下文对象中属性操作。

            2.HttpSession:监听Servelt的会话信息。                 

                  public interface HttpSessionListener{
                        public void sessionCreated(HttpSessionEvent sc);会话对象被创建时调用此方法
                        public void sessionDestroyed(HttpSessionEvent sc);会话对象被释放时调用此方法
                    }

                            HttpSessionEvent是会话事件类,其声明如下:
                            public class HttpSessionEvent extends java.util.EventObject
                        包含方法:
                            HttpSession getSession( )返回产生事件的Session对象。 

           3.ServletRequest:监听Servlet的请求信息    

猜你喜欢

转载自blog.csdn.net/u012060033/article/details/82698339