servlet过滤器与servlet监听器吴光伟servlet过滤器与servlet监听器•1、servlet过滤器•2、servlet监听器1、servlet过滤器•1.1、什么是servlet过滤器•1.2、servlet过滤器的开发步骤•1.3、实例•1.4、servlet过滤器的深入使用1.1、什么是jsp监听器•servlet过滤器(Filter)是小型的Web组件,它们拦截请求和响应,以便查看、提取或以某种方式操作正在客户机和服务器之间交换的数据。•实现过滤器只须在web.xml中设定相关设定,不需要修改Servlet、JSP和其他静态页面。因此开发者可以轻易的加入Filter机制。•过滤器是通常封装了一些功能的Web组件,这些功能虽然很重要,但是对于处理客户机请求或发送响应来说不是决定性的。•过滤器在Web开发中的一些主要应用:•·对用户请求进行统一认证。•·对用户的访问请求进行记录和审核。•·对用户发送的数据进行过滤或替换。•·转换图象格式。•·对响应内容进行压缩,减少传输量。•·对请求或响应进行加解密处理。•·触发资源访问是事件。1.2、servlet过滤器的开发步骤•开发Servlet过滤器的步骤如下:•1.2.1编写实现Filter接口的Servlet类。•1.2.2在web.xml中配置Filter。1.2.1编写实现Filter接口的Servlet类•开发一个过滤器需要实现Filter接口,Filter接口定义了以下方法:•(1)publicvoidinit(FilterConfigfilterConfig)throwsServletException•由Web容器调用,初始化此Filter。•(2)publicvoiddoFilter(ServletRequestrequest,ServletResponse•response,FilterChainchain)throwsjava.io.IOException,ServletException•具体过滤处理代码。•(3)publicvoiddestroy()•由Web容器调用,在过滤器被销毁之前调用。•doFilter():•与servlet拥有一个service()方法(这个方法又调用doPost()或者doGet())来处理请求一样,过滤器拥有单个用于处理请求和响应的方法doFilter()。•这个方法接受三个输入参数:一个ServletRequest、ServletResponse和一个FilterChain对象。•packagecom.wgw.filter;•importjava.io.IOException;•importjavax.servlet.Filter;•importjavax.servlet.FilterChain;•importjavax.servlet.FilterConfig;•importjavax.servlet.ServletException;•importjavax.servlet.ServletRequest;•importjavax.servlet.ServletResponse;•publicclassSimpleFilterimplementsFilter{•privateFilterConfigfilterConfig;••publicvoidinit(FilterConfigconfig)throwsServletException{•this.filterConfig=config;•}1.publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)2.throwsjava.io.IOException,ServletException{3.System.out.println(WithinSimpleFilter1:FilteringtheRequest...1);4.//以上是请求资源之前进行拦截后的操作5.chain.doFilter(request,response);//提交给相应资源6.//以上是响应到客户端之前拦截后的操作7.System.out.println(WithinSimpleFilter1:FilteringtheResponse...1);8.}9.10.publicvoiddestroy(){11.this.filterConfig=null;12.}13.}1.2.2在web.xml中配置Filter•filter•filter-namefilter1/filter-name•filter-classcom.wgw.filter.SimpleFilter1/filter-class•/filter•filter-mapping•filter-namefilter1/filter-name•url-pattern/*/url-pattern•/filter-mapping效果•输入网址思考•1、比较init方法与构造方法的区别。•2、是否与servlet相同,即只生成一个实例,所有的用户都调用这个实例的dofilter方法?1.3、实例•实例一•记录所有用户访问try网站的信息。•packagecom.wgw.filter;•importjava.io.IOException;•importjavax.servlet.*;•importjava.util.*;•importjavax.servlet.http.*;•publicclassSimpleFilterimplementsFilter{•privateFilterConfigfilterConfig;••publicvoidinit(FilterConfigconfig)throwsServletException{•this.filterConfig=config;•}••publicvoiddestroy(){•this.filterConfig=null;•}•publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)•throwsjava.io.IOException,ServletException{•HttpServletRequestreq=(HttpServletRequest)request;•ServletContextapplication=filterConfig.getServletContext();•application.log(req.getRemoteHost()+triedtoaccess+req.getRequestURL()+on+newDate()+.);•chain.doFilter(request,response);•}•}效果•实例二•用于设置HTTP请求字符编码的过滤器,通过过滤器参数encoding指明使用何种字符编码,用于处理HtmlForm请求参数的中文问题•packagecom.wgw.filter;•importjavax.servlet.*;•importjava.io.IOException;••publicclassCharacterEncodingFilter•implementsFilter{•protectedFilterConfigfilterConfig=null;•protectedStringencoding=;•publicvoidinit(FilterConfigfilterConfig)throwsServletException•{•this.filterConfig=filterConfig;•this.encoding=filterConfig.getInitParameter(encoding);•}•publicvoiddoFilter(ServletRequestservletRequest,ServletResponseservletResponse,FilterChainfilterChain)throwsIOException,ServletException{•if(encoding!=null)•servletRequest.setCharacterEncoding(encoding);•filterChain.doFilter(servletRequest,servletResponse);•}•publicvoiddestroy(){•filterConfig=null;•encoding=null;•}••}Web.xml•filter•filter-nameCharacterEncodingFilter/filter-name•filter-classcom.wgw.filter.CharacterEncodingFilter/filter-class•init-param•param-nameencoding/param-name•param-valuegbk/param-value•/init-param•/filter••filter-mapping•filter-nameCharacterEncodingFilter/filter-name•url-pattern/*/url-pattern•/filter-mapping•实例三•创建Servlet过滤器“IPFilter.java”,它可以过滤用户的IP地址,以进行访问控制。•(1)源代码•publicclassIPFilterimplementsFilter{•protectedFilterConfigfilterConfig;•protectedStringfilterIP;//需要过滤的IP地址•/*初始化过滤器*/•publicvoidinit(FilterConfigconfig)throwsServletException{•this.filterConfig=config;•filterIP=config.getInitParameter(filterIP);//获取被过滤的IP地址•if(filterIP==null)filterIP=;•}•/*过滤操作*/•publicvoiddoFilter(ServletRequestreg,ServletResponseres,•FilterChainchain)throwsIOException,ServletException{•RequestDispatcherreqDispatcher=reg.getRequestDispatcher(error.jsp);•StringremoteIP=reg.getRemoteAddr();//获取本地IP地址•if(remoteIP.equals(filterIP)){//如果该IP地址被过滤,将转向错误页面•reqDispatcher.forward(reg,res);•}else{//否则将请求转发给过滤器链中的其他资源•chain.doFilter(reg,res);•}•}•/*销毁过滤器*/•publicvoiddestroy(){•this.filterConfig=null;•}•}•(2)在web.xml文件中配置IPFilter过滤器,在配置文件中定义了一个名为filterIP的参数,它的值为192.168.70.82,表示IP地址为192.168.70.82的用户将被拒绝访问。在web.xml文件中配置过滤器如下:•!--配置过滤器--•filter•filter-nameIPFil