Filter学习(二)Filter使用Decorator设计模式
在filter中可以得到代表用户请求和响应的request、response对象,因此在编程中可以使用Decorator(装饰器)模式对request、response对象进行包装,再把包装对象传给目标资源,从而实现一些特殊需求。
一、Decorator设计模式
1.1、Decorator设计模式介绍
当某个对象的方法不适应业务需求时,通常有2种方式可以对方法进行增强:
- 编写子类,覆盖需增强的方法。
- 使用Decorator设计模式对方法进行增强。
在阎宏博士的《JAVA与模式》一书中开头是这样描述装饰(Decorator)模式的:装饰模式又名包装(Wrapper)模式。装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案。装饰模式是在不必改变原类文件和使用继承的情况下,动态的扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。
那么在实际应用中遇到需增强对象的方法时,到底选用哪种方式比较好呢?这个没有具体的定式,只能是根据具体的需求来采用具体的方式,不过有一种情况下,必须使用Decorator设计模式:即被增强的对象,开发人员只能得到它的对象,无法得到它的class文件。比如request、response对象,开发人员之所以在servlet中能通过sun公司定义的HttpServletRequest\response接口去操作这些对象,是因为Tomcat服务器厂商编写了request、response接口的实现类。web服务器在调用servlet时,会用这些接口的实现类创建出对象,然后传递给servlet程序。此种情况下,由于开发人员根本不知道服务器厂商编写的request、response接口的实现类是哪个?在程序中只能拿到服务器厂商提供的对象,因此就只能采用Decorator设计模式对这些对象进行增强。
1.2、Decorator设计模式的实现
1.首先看需要被增强对象继承了什么接口或父类,编写一个类也去继承这些接口或父类。
2.在类中定义一个变量,变量类型即需增强对象的类型。
3.在类中定义一个构造函数,接收需增强的对象。
4.覆盖需增强的方法,编写增强的代码。
二、使用Decorator设计模式增强request对象
Servlet API 中提供了一个request对象的Decorator设计模式的默认实现类HttpServletRequestWrapper,HttpServletRequestWrapper 类实现了request 接口中的所有方法,但这些方法的内部实现都是仅仅调用了一下所包装的的 request 对象的对应方法,以避免用户在对request对象进行增强时需要实现request接口中的所有方法。
2.1、使用Decorator模式包装request对象解决get和post请求方式下的中文乱码问题
编写一个用于处理中文乱码的过滤器CharacterEncodingFilter,代码如下:
package com.web.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; /** * @ClassName: CharacterEncodingFilter * @Description: 此过滤器用来解决解决get、post请求方式下的中文乱码问题 * @author: hdb * @date: 2017-8-31 下午11:09:37 * */ public class CharacterEncodingFilter implements Filter { private FilterConfig filterConfig = null; //设置默认的字符编码 private String defaultCharset = "UTF-8"; public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; //得到在web.xml中配置的字符编码 String charset = filterConfig.getInitParameter("charset"); if(charset==null){ charset = defaultCharset; } request.setCharacterEncoding(charset); response.setCharacterEncoding(charset); response.setContentType("text/html;charset="+charset); MyCharacterEncodingRequest requestWrapper = new MyCharacterEncodingRequest(request); chain.doFilter(requestWrapper, response); } public void init(FilterConfig filterConfig) throws ServletException { //得到过滤器的初始化配置信息 this.filterConfig = filterConfig; } public void destroy() { } } /** * @ClassName: MyCharacterEncodingRequest * @Description: Servlet API中提供了一个request对象的Decorator设计模式的默认实现类HttpServletRequestWrapper, * (HttpServletRequestWrapper类实现了request接口中的所有方法,但这些方法的内部实现都是仅仅调用了一下所包装的的 request对象的对应方法) * 以避免用户在对request对象进行增强时需要实现request接口中的所有方法。 * 所以当需要增强request对象时,只需要写一个类继承HttpServletRequestWrapper类,然后在重写需要增强的方法即可 * @author: hdb * @date: 2017-9-2 下午10:42:57 * 1.实现与被增强对象相同的接口 2、定义一个变量记住被增强对象 3、定义一个构造函数,接收被增强对象 4、覆盖需要增强的方法 5、对于不想增强的方法,直接调用被增强对象(目标对象)的方法 */ class MyCharacterEncodingRequest extends HttpServletRequestWrapper{ //定义一个变量记住被增强对象(request对象是需要被增强的对象) private HttpServletRequest request; //定义一个构造函数,接收被增强对象 public MyCharacterEncodingRequest(HttpServletRequest request) { super(request); this.request = request; } /* 覆盖需要增强的getParameter方法 * @see javax.servlet.ServletRequestWrapper#getParameter(java.lang.String) */ @Override public String getParameter(String name) { try{ //获取参数的值 String value= this.request.getParameter(name); if(value==null){ return null; } //如果不是以get方式提交数据的,就直接返回获取到的值 if(!this.request.getMethod().equalsIgnoreCase("get")) { return value; }else{ //如果是以get方式提交数据的,就对获取到的值进行转码处理 value = new String(value.getBytes("ISO8859-1"),this.request.getCharacterEncoding()); return value; } }catch (Exception e) { throw new RuntimeException(e); } } }
在web.xml文件中配置CharacterEncodingFilter:
<!--配置字符过滤器,解决get、post请求方式下的中文乱码问题--> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>com.web.filter.CharacterEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
编写jsp测试页面,如下:
<%@ page language="java" pageEncoding="UTF-8"%> <%--引入jstl标签库 --%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE HTML> <html> <head> <title>使用字符过滤器解决解决get、post请求方式下的中文乱码问题</title> </head> <body> <%--使用c:url标签构建url,构建好的url存储在servletDemo1变量中--%> <c:url value="/servlet/ServletDemo1" scope="page" var="servletDemo1"> <%--构建的url的附带的中文参数 ,参数名是:username,值是:hdb--%> <c:param name="username" value="hdb"></c:param> </c:url> <%--使用get的方式访问 --%> <a href="${servletDemo1}">超链接(get方式请求)</a> <hr/> <%--使用post方式提交表单 --%> <form action="${pageContext.request.contextPath}/servlet/ServletDemo1" method="post"> 用户名:<input type="text" name="username" value="hdb" /> <input type="submit" value="post方式提交"> </form> </body> </html>
编写处理用户请求的ServletDemo1:
package com.web.controller; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletDemo1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //接收参数 String username = request.getParameter("username"); //获取请求方式 String method = request.getMethod(); //获取输出流 PrintWriter out = response.getWriter(); out.write("请求的方式:"+method); out.write("<br/>"); out.write("接收到的参数:"+username); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
测试结果如下:
从运行结果中可以看到,无论是get请求方式还是post请求方式,中文乱码问题都可以完美解决了。
2.2、使用Decorator模式包装request对象实现html标签转义功能
编写一个html转义过滤器,代码如下:
package com.web.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; /** * @ClassName: HtmlFilter * @Description: html转义过滤器 * @author: hdb * @date: 2017-9-2 下午11:28:41 * */ public class HtmlFilter implements Filter { public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; MyHtmlRequest myrequest = new MyHtmlRequest(request); chain.doFilter(myrequest, response); } public void destroy() { } public void init(FilterConfig filterConfig) throws ServletException { } } /** * @ClassName: MyHtmlRequest * @Description: 使用Decorator模式包装request对象,实现html标签转义功能 * @author: hdb * @date: 2017-9-2 下午11:29:09 * */ class MyHtmlRequest extends HttpServletRequestWrapper { private HttpServletRequest request; public MyHtmlRequest(HttpServletRequest request) { super(request); this.request = request; } /* 覆盖需要增强的getParameter方法 * @see javax.servlet.ServletRequestWrapper#getParameter(java.lang.String) */ @Override public String getParameter(String name) { String value = this.request.getParameter(name); if (value == null) { return null; } //调用filter转义value中的html标签 return filter(value); } /** * @Method: filter * @Description: 过滤内容中的html标签 * @Anthor:hdb * @param message * @return */ public String filter(String message) { if (message == null){ return null; } char content[] = new char[message.length()]; message.getChars(0, message.length(), content, 0); StringBuffer result = new StringBuffer(content.length + 50); for (int i = 0; i < content.length; i++) { switch (content[i]) { case '<': result.append("<"); break; case '>': result.append(">"); break; case '&': result.append("&"); break; case '"': result.append("""); break; default: result.append(content[i]); } } return result.toString(); } }
在web.xml文件中配置HtmlFilter:
<!--配置Html过滤器,转义内容中的html标签--> <filter> <filter-name>HtmlFilter</filter-name> <filter-class>com.web.filter.HtmlFilter</filter-class> </filter> <filter-mapping> <filter-name>HtmlFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
编写jsp测试页面,如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML> <html> <head> <title>html过滤器测试</title> </head> <body> <form action="${pageContext.request.contextPath}/servlet/ServletDemo2" method="post"> 留言: <textarea rows="8" cols="70" name="message"> <script type="text/javascript"> while(true){ alert("死循环了,我会不停地弹出了"); } </script> <a href="http://www.cnblogs.com">访问博客园</a> </textarea> <input type="submit" value="发表"> </form> </body> </html>
编写处理用户请求的ServletDemo2:
package com.web.controller; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletDemo2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取用户输入的内容 String message = request.getParameter("message"); response.getWriter().write("您上次的留言是:<br/>" + message); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
测试结果如下:
从运行结果中可以看到,所有的html标签都被转义输出了。
2.3、使用Decorator模式包装request对象实现敏感字符过滤功能
编写一个敏感字符过滤器,代码如下:
package com.web.filter; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; /** * @ClassName: DirtyFilter * @Description: 敏感词过滤器 * @author: hdb * @date: 2017-9-6 上午10:43:11 * */ public class DirtyFilter implements Filter { private FilterConfig config = null; @Override public void init(FilterConfig filterConfig) throws ServletException { this.config = filterConfig; } @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; DirtyRequest dirtyrequest = new DirtyRequest(request); chain.doFilter(dirtyrequest, response); } @Override public void destroy() { } /** * @Method: getDirtyWords * @Description: 获取敏感字符 * @Anthor:hdb * * @return */ private List<String> getDirtyWords(){ List<String> dirtyWords = new ArrayList<String>(); String dirtyWordPath = config.getInitParameter("dirtyWord"); InputStream inputStream = config.getServletContext().getResourceAsStream(dirtyWordPath); InputStreamReader is = null; try { is = new InputStreamReader(inputStream,"UTF-8"); } catch (UnsupportedEncodingException e2) { e2.printStackTrace(); } BufferedReader reader = new BufferedReader(is); String line; try { while ((line = reader.readLine())!= null) {//如果 line为空说明读完了 dirtyWords.add(line); } } catch (IOException e) { e.printStackTrace(); } return dirtyWords; } /** * @ClassName: DirtyRequest * @Description: 使用Decorator模式包装request对象,实现敏感字符过滤功能 * @author: hdb * @date: 2017-9-6 上午11:56:35 * */ class DirtyRequest extends HttpServletRequestWrapper{ private List<String> dirtyWords = getDirtyWords(); private HttpServletRequest request; public DirtyRequest(HttpServletRequest request) { super(request); this.request = request; } /* 重写getParameter方法,实现对敏感字符的过滤 * @see javax.servlet.ServletRequestWrapper#getParameter(java.lang.String) */ @Override public String getParameter(String name) { String value = this.request.getParameter(name); if(value==null){ return null; } for(String dirtyWord : dirtyWords){ if(value.contains(dirtyWord)){ System.out.println("内容中包含敏感词:"+dirtyWord+",将会被替换成****"); //替换敏感字符 value = value.replace(dirtyWord, "****"); } } return value; } } }
在web.xml文件中配置DirtyFilter:
<!--配置敏感字符过滤器--> <filter> <filter-name>DirtyFilter</filter-name> <filter-class>com.web.filter.DirtyFilter</filter-class> <!-- 配置要过滤的敏感字符文件 --> <init-param> <param-name>dirtyWord</param-name> <param-value>/WEB-INF/DirtyWord.txt</param-value> </init-param> </filter> <filter-mapping> <filter-name>DirtyFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
当用户填写的内容包含一些敏感字符时,在DirtyFilter过滤器中就会将这些敏感字符替换掉。
我们如果将上述的CharacterEncodingFilter、HtmlFilter、DirtyFilter这三个过滤器联合起来使用,那么就相当于是把request对象包装了3次,request对象的getParameter方法经过3次重写,使得getParameter方法的功能大大增强,可以同时解决中文乱码,html标签转义,敏感字符过滤这些需求。
在实际开发中完全可以将上述的三个过滤器合并成一个,让合并后的过滤器具有解决中文乱码,html标签转义,敏感字符过滤这些功能,例如:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.web.filter; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; /** * @ClassName: AdvancedFilter * @Description: 这个过滤器是用来解决中文乱码,转义内容中的html标签,过滤内容中的敏感字符的 * @author: 孤傲苍狼 * @date: 2014-9-6 下午6:17:37 * */ public class AdvancedFilter implements Filter { private FilterConfig filterConfig = null; //设置默认的字符编码 private String defaultCharset = "UTF-8"; @Override public void init(FilterConfig filterConfig) throws ServletException { //得到过滤器的初始化配置信息 this.filterConfig = filterConfig; } @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; //得到在web.xml中配置的字符编码 String charset = filterConfig.getInitParameter("charset"); if(charset==null){ charset = defaultCharset; } request.setCharacterEncoding(charset); response.setCharacterEncoding(charset); response.setContentType("text/html;charset="+charset); AdvancedRequest requestWrapper = new AdvancedRequest(request); chain.doFilter(requestWrapper, response); } @Override public void destroy() { } class AdvancedRequest extends HttpServletRequestWrapper{ private List<String> dirtyWords = getDirtyWords(); //定义一个变量记住被增强对象(request对象是需要被增强的对象) private HttpServletRequest request; //定义一个构造函数,接收被增强对象 public AdvancedRequest(HttpServletRequest request) { super(request); this.request = request; } /* 覆盖需要增强的getParameter方法 * @see javax.servlet.ServletRequestWrapper#getParameter(java.lang.String) */ @Override public String getParameter(String name) { try{ //获取参数的值 String value= this.request.getParameter(name); if(value==null){ return null; } //如果不是以get方式提交数据的,就直接返回获取到的值 if(!this.request.getMethod().equalsIgnoreCase("get")) { //调用filter转义value中的html标签 value= filter(value); }else{ //如果是以get方式提交数据的,就对获取到的值进行转码处理 value = new String(value.getBytes("ISO8859-1"),this.request.getCharacterEncoding()); //调用filter转义value中的html标签 value= filter(value); } for(String dirtyWord : dirtyWords){ if(value.contains(dirtyWord)){ System.out.println("内容中包含敏感词:"+dirtyWord+",将会被替换成****"); //替换敏感字符 value = value.replace(dirtyWord, "****"); } } return value; }catch (Exception e) { throw new RuntimeException(e); } } } /** * @Method: filter * @Description: 过滤内容中的html标签 * @Anthor:孤傲苍狼 * @param value * @return */ public String filter(String value) { if (value == null){ return null; } char content[] = new char[value.length()]; value.getChars(0, value.length(), content, 0); StringBuffer result = new StringBuffer(content.length + 50); for (int i = 0; i < content.length; i++) { switch (content[i]) { case '<': result.append("<"); break; case '>': result.append(">"); break; case '&': result.append("&"); break; case '"': result.append("""); break; default: result.append(content[i]); } } return (result.toString()); } /** * @Method: getDirtyWords * @Description: 获取敏感字符 * @Anthor:孤傲苍狼 * * @return */ private List<String> getDirtyWords(){ List<String> dirtyWords = new ArrayList<String>(); String dirtyWordPath = filterConfig.getInitParameter("dirtyWord"); InputStream inputStream = filterConfig.getServletContext().getResourceAsStream(dirtyWordPath); InputStreamReader is = null; try { is = new InputStreamReader(inputStream,defaultCharset); } catch (UnsupportedEncodingException e2) { e2.printStackTrace(); } BufferedReader reader = new BufferedReader(is); String line; try { while ((line = reader.readLine())!= null) {//如果 line为空说明读完了 dirtyWords.add(line); } } catch (IOException e) { e.printStackTrace(); } return dirtyWords; } }
在web.xml文件中配置AdvancedFilter:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<filter> <filter-name>AdvancedFilter</filter-name> <filter-class>com.web.filter.AdvancedFilter</filter-class> <init-param> <param-name>charset</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>dirtyWord</param-name> <param-value>/WEB-INF/DirtyWord.txt</param-value> </init-param> </filter> <filter-mapping> <filter-name>AdvancedFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
AdvancedFilter过滤器同时具有解决中文乱码,转义内容中的html标签,过滤内容中的敏感字符这些功能。
三、使用Decorator设计模式增强response对象
Servlet API 中提供了response对象的Decorator设计模式的默认实现类HttpServletResponseWrapper ,HttpServletResponseWrapper类实现了response接口中的所有方法,但这些方法的内部实现都是仅仅调用了一下所包装的的 response对象的对应方法,以避免用户在对response对象进行增强时需要实现response接口中的所有方法。
3.1、response增强案例——压缩响应正文内容
应用HttpServletResponseWrapper对象,压缩响应正文内容。
具体思路:通过filter向目标页面传递一个自定义的response对象。在自定义的response对象中,重写getOutputStream方法和getWriter方法,使目标资源调用此方法输出页面内容时,获得的是我们自定义的ServletOutputStream对象。在我们自定义的ServletOuputStream对象中,重写write方法,使写出的数据写出到一个buffer中。当页面完成输出后,在filter中就可得到页面写出的数据,从而我们可以调用GzipOuputStream对数据进行压缩后再写出给浏览器,以此完成响应正文件压缩功能。
编写压缩过滤器,代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.web.filter; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.zip.GZIPOutputStream; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; /** * @ClassName: GzipFilter * @Description: 压缩过滤器,将web应用中的文本都经过压缩后再输出到浏览器 * @author: 孤傲苍狼 * @date: 2014-9-7 上午10:52:42 * */ public class GzipFilter implements Filter { public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; BufferResponse myresponse = new BufferResponse(response); chain.doFilter(request, myresponse); //拿出缓存中的数据,压缩后再打给浏览器 byte out[] = myresponse.getBuffer(); System.out.println("原始大小:" + out.length); ByteArrayOutputStream bout = new ByteArrayOutputStream(); //压缩输出流中的数据 GZIPOutputStream gout = new GZIPOutputStream(bout); gout.write(out); gout.close(); byte gzip[] = bout.toByteArray(); System.out.println("压缩后的大小:" + gzip.length); response.setHeader("content-encoding", "gzip"); response.setContentLength(gzip.length); response.getOutputStream().write(gzip); } public void destroy() { } public void init(FilterConfig filterConfig) throws ServletException { } } class BufferResponse extends HttpServletResponseWrapper{ private ByteArrayOutputStream bout = new ByteArrayOutputStream(); private PrintWriter pw; private HttpServletResponse response; public BufferResponse(HttpServletResponse response) { super(response); this.response = response; } @Override public ServletOutputStream getOutputStream() throws IOException { return new MyServletOutputStream(bout); } @Override public PrintWriter getWriter() throws IOException { pw = new PrintWriter(new OutputStreamWriter(bout,this.response.getCharacterEncoding())); return pw; } public byte[] getBuffer(){ try{ if(pw!=null){ pw.close(); } if(bout!=null){ bout.flush(); return bout.toByteArray(); } return null; }catch (Exception e) { throw new RuntimeException(e); } } } class MyServletOutputStream extends ServletOutputStream{ private ByteArrayOutputStream bout; public MyServletOutputStream(ByteArrayOutputStream bout){ this.bout = bout; } @Override public void write(int b) throws IOException { this.bout.write(b); } }
在web.xml中配置压缩过滤器:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<filter> <description>配置压缩过滤器</description> <filter-name>GzipFilter</filter-name> <filter-class>com.web.filter.GzipFilter</filter-class> </filter> <!--jsp文件的输出的内容都经过压缩过滤器压缩后才输出 --> <filter-mapping> <filter-name>GzipFilter</filter-name> <url-pattern>*.jsp</url-pattern> <!-- 配置过滤器的拦截方式--> <!-- 对于在Servlet中通过 request.getRequestDispatcher("jsp页面路径").forward(request, response) 方式访问的Jsp页面的要进行拦截 --> <dispatcher>FORWARD</dispatcher> <!--对于直接以URL方式访问的jsp页面进行拦截,过滤器的拦截方式默认就是 REQUEST--> <dispatcher>REQUEST</dispatcher> </filter-mapping> <!--js文件的输出的内容都经过压缩过滤器压缩后才输出 --> <filter-mapping> <filter-name>GzipFilter</filter-name> <url-pattern>*.js</url-pattern> </filter-mapping> <!--css文件的输出的内容都经过压缩过滤器压缩后才输出 --> <filter-mapping> <filter-name>GzipFilter</filter-name> <url-pattern>*.css</url-pattern> </filter-mapping> <!--html文件的输出的内容都经过压缩过滤器压缩后才输出 --> <filter-mapping> <filter-name>GzipFilter</filter-name> <url-pattern>*.html</url-pattern> </filter-mapping>
3.2、response增强案例——缓存数据到内存
对于页面中很少更新的数据,例如商品分类,为避免每次都要从数据库查询分类数据,因此可把分类数据缓存在内存或文件中,以此来减轻数据库压力,提高系统响应速度。
编写缓存数据的过滤器,代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.web.filter; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; /** * @ClassName: WebResourceCachedFilter * @Description: Web资源缓存过滤器 * @author: 孤傲苍狼 * @date: 2014-9-8 上午12:20:16 * */ public class WebResourceCachedFilter implements Filter { /** * @Field: map * 缓存Web资源的Map容器 */ private Map<String,byte[]> map = new HashMap<String,byte[]>(); @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; //1.得到用户请求的uri String uri = request.getRequestURI(); //2.看缓存中有没有uri对应的数据 byte b[] = map.get(uri); //3.如果缓存中有,直接拿缓存的数据打给浏览器,程序返回 if(b!=null){ //根据字节数组和指定的字符编码构建字符串 String webResourceHtmlStr = new String(b,response.getCharacterEncoding()); System.out.println(webResourceHtmlStr); response.getOutputStream().write(b); return; } //4.如果缓存没有,让目标资源执行,并捕获目标资源的输出 BufferResponse myresponse = new BufferResponse(response); chain.doFilter(request, myresponse); //获取缓冲流中的内容的字节数组 byte out[] = myresponse.getBuffer(); //5.把资源的数据以用户请求的uri为关键字保存到缓存中 map.put(uri, out); //6.把数据打给浏览器 response.getOutputStream().write(out); } @Override public void destroy() { } class BufferResponse extends HttpServletResponseWrapper{ private ByteArrayOutputStream bout = new ByteArrayOutputStream(); //捕获输出的缓存 private PrintWriter pw; private HttpServletResponse response; public BufferResponse(HttpServletResponse response) { super(response); this.response = response; } @Override public ServletOutputStream getOutputStream() throws IOException { return new MyServletOutputStream(bout); } @Override public PrintWriter getWriter() throws IOException { pw = new PrintWriter(new OutputStreamWriter(bout,this.response.getCharacterEncoding())); return pw; } public byte[] getBuffer(){ try{ if(pw!=null){ pw.close(); } return bout.toByteArray(); }catch (Exception e) { throw new RuntimeException(e); } } } class MyServletOutputStream extends ServletOutputStream{ private ByteArrayOutputStream bout; public MyServletOutputStream(ByteArrayOutputStream bout){ //接收数据写到哪里 this.bout = bout; } @Override public void write(int b) throws IOException { bout.write(b); } } }
在web.xml中配置Web资源缓存过滤器:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<filter> <description>Web资源缓存过滤器</description> <filter-name>WebResourceCachedFilter</filter-name> <filter-class>com.web.filter.WebResourceCachedFilter</filter-class> </filter> <filter-mapping> <filter-name>WebResourceCachedFilter</filter-name> <!-- 映射需要缓存输出的JSP页面,这几个页面都只是单纯作为输入UI,不会有太多的变化,因此可以缓存输出 --> <url-pattern>/login.jsp</url-pattern> <url-pattern>/test.jsp</url-pattern> <url-pattern>/test2.jsp</url-pattern> </filter-mapping>