您好,欢迎访问三七文档
过滤器目标描述过滤器理解过滤器生命周期实现简单的过滤器过滤器简介过滤器是向Web应用程序的请求和响应处理添加功能的Web服务组件过滤器的工作原理:Web资源用户发送请求过滤器将用户的请求发送至Web资源资源将响应发送至过滤器过滤器将Web资源的响应发送给用户用户过滤器过滤器生命周期过滤器生命周期的各个阶段:实例化初始化过滤销毁Filter接口过滤器是一个对象,它根据用户的请求和资源的响应进行过滤Filter接口包含以下方法:Filter接口init()doFilter()destroy()过滤器链FilterChain接口用于调用过滤器链中的一系列过滤器用户过滤器1过滤器2过滤器3Web资源过滤器链FilterConfig在初始化过程中,容器使用FilterConfig将信息传递给过滤器FilterConfig接口包含以下方法:getFilterName()getInitParameter()getInitParameterNames()getServletContext()FilterConfig过滤器定义过滤器定义用于将过滤器名称与部署描述符中的特定类关联filterfilter-nameFilterName/filter-namefilter-classClassName/filter-class/filter指定过滤器的名称指定类的名称过滤器映射使用filter-mapping元素可将过滤器映射至Web资源,如Servletfilter-mappingfilter-nameMyFilter/filter-nameurl-pattern/servletname/*/url-pattern/filter-mapping指定过滤器的名称指定Servlet的名称过滤器定义和映射定义和映射过滤器部署描述符中filter和filter-mapping元素的用法?xmlversion=1.0encoding=UTF-8?!DOCTYPEweb-appPUBLIC-//SunMicrosystems,Inc.//DTDWebApplication2.3//EN*/url-pattern/filter-mapping/web-app创建Servlet演示:示例1...out.println(headtitleLoggerServlet/title/head);out.println(bodybgcolor=\#ffffff\);out.println(“h1简单过滤器Servlet/h1);StringrequestedUri=(String)request.getAttribute(requestURI);out.println(“b请求的URI为:/b+requestedUri);out.println(/body);out.println(/html);...创建过滤器演示:示例2FilterConfig、doFilter()、init()和destroy()方法的用法publicclassLoggerFilterextendsHttpServletimplementsFilter{privateFilterConfigfilterConfig;publicvoidinit(FilterConfigfilterConfig)throwsServletException{this.filterConfig=filterConfig;}publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainfilterChain){longstartTime=System.currentTimeMillis();StringremoteAddress=request.getRemoteAddr();StringremoteHost=request.getRemoteHost();HttpServletRequestmyReq=(HttpServletRequest)request;StringreqURI=myReq.getRequestURI();System.out.println(reqURI);创建HTML文件演示:示例3htmlheadtitle新文档/title/headbodyh1欢迎使用第一个过滤器示例/h1ahref=‘/FilterModule/loggerservlet’单击此处查看过滤器的效果/a/body/html实现过滤器链-过滤器1演示:示例4publicclassSimpleFilterextendsHttpServletimplementsFilter{privateFilterConfigfilterConfig;publicvoidinit(FilterConfigfilterConfig)throwsServletException{this.filterConfig=filterConfig;}publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainfilterChain)try{filterConfig.getServletContext().log(inSimpleFilter);System.out.println(inSimpleFilter);filterChain.doFilter(request,response);filterConfig.getServletContext().log(GettingoutofSimpleFilter);System.out.println(GettingoutofSimpleFilter);}catch(ServletExceptionsx){...}演示:示例5实现过滤器链-过滤器2publicclassSimpleFilter2extendsHttpServletimplementsFilter{privateFilterConfigfilterConfig;...publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainfilterChain){try{filterConfig.getServletContext().log(inSimpleFilter2);System.out.println(inSimpleFilter2);filterChain.doFilter(request,response);filterConfig.getServletContext().log(GettingoutofSimpleFilter2);System.out.println(GettingoutofSimpleFilter2);}catch(ServletExceptionsx){...}...}实现过滤器链-web.xmlfilterfilter-namesimplefilter/filter-namefilter-classmyfilter.SimpleFilter/filter-class/filterfilter-mappingfilter-namesimplefilter/filter-nameurl-pattern/*/url-pattern/filter-mappingfilterfilter-namesimplefilter2/filter-namefilter-classmyfilter.SimpleFilter2/filter-class/filterfilter-mappingfilter-namesimplefilter2/filter-nameurl-pattern/*/url-pattern/filter-mapping实现过滤器链htmlhead/headbodyh1欢迎访问过滤器链演示程序!/h1/body/html创建一个html文件作为Web资源总结过滤器是Web服务组件过滤器可以访问客户端输入的请求和Web资源输出的响应过滤器定义用于将过滤器名称与特定的类关联在一起过滤器映射用于将过滤器映射至Web资源Filter接口包含各种方法,如init()、doFilter()和destroy()每次用户发送请求或Web资源发送响应时都会调用doFilter()方法FilterChain接口用于调用过滤器链中的下一个过滤器在初始化过程中,Servlet使用FilterConfig将信息传递给过滤器
本文标题:java过滤器
链接地址:https://www.777doc.com/doc-3399840 .html