java在过滤器中修改一个http请求的返回内容response保存了请求的返回信息,里面有个outputstream,你要返回给页面的流,都在这个地方保存.sun为我们提供了这么一个工具HttpServletResponseWrapper抽象类,利用这个类的子类把servletresponse包装一下,在过滤器中使用,就可以去除response的文件流,对其作出修改.首先要先子类化HttpServletResponseWrapper。先子类化HttpServletResponseWrapper1、获取response对象,并将输出存放在自定义的流里面,那么关于输出流(outputStream、writer)的操作都是需要重写的了:1)以流的方式获取输出——重写getOutputStrem()2)以字符方式获取输出——重写getWriter()3)刷新流——重写flushBuffer()4)重置流——重写reset()然后加入新增的获取输出数据的方法就ok了。2、定义response包装器WapperedResponse继承HttpServletResponseWrapperimportjava.io.ByteArrayOutputStream;importjava.io.CharArrayWriter;importjava.io.IOException;importjava.io.PrintWriter;importjava.io.UnsupportedEncodingException;importjavax.servlet.ServletOutputStream;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpServletResponseWrapper;publicclassResponseWrapperextendsHttpServletResponseWrapper{privatePrintWritercachedWriter;privateCharArrayWriterbufferedWriter;privateServletOutputStreamout=null;publicResponseWrapper(HttpServletResponseresponse)throwsIOException{super(response);//这个是我们保存返回结果的地方bufferedWriter=newCharArrayWriter();out=newWapperedOutputStream(bufferedWriter);//这个是包装PrintWriter的,让所有结果通过这个PrintWriter写入到bufferedWriter中cachedWriter=newPrintWriter(bufferedWriter);}//重载父类获取writer的方法@OverridepublicPrintWritergetWriter()throwsUnsupportedEncodingException{returncachedWriter;}//重载父类获取outputstream的方法@OverridepublicServletOutputStreamgetOutputStream()throwsIOException{returnout;}//重载父类获取flushBuffer的方法@OverridepublicvoidflushBuffer()throwsIOException{if(out!=null){out.flush();}if(cachedWriter!=null){cachedWriter.flush();}}@Overridepublicvoidreset(){bufferedWriter.reset();}/***获取原始的HTML页面内容。*@return*/publicStringgetResult()throwsIOException{flushBuffer();returnbufferedWriter.toString();}//内部类,对ServletOutputStream进行包装privateclassWapperedOutputStreamextendsServletOutputStream{privateCharArrayWriterbos=null;publicWapperedOutputStream(CharArrayWriterstream)throwsIOException{bos=stream;}@Overridepublicvoidwrite(intb)throwsIOException{bos.write(b);}}}然后在过滤器中使用,如下:importjava.io.IOException;importjava.io.PrintWriter;importjava.util.regex.Matcher;importjava.util.regex.Pattern;importjavax.servlet.Filter;importjavax.servlet.FilterChain;importjavax.servlet.FilterConfig;importjavax.servlet.ServletException;importjavax.servlet.ServletRequest;importjavax.servlet.ServletResponse;importjavax.servlet.http.HttpServletResponse;publicclassInitLabelParamSidFilterimplementsFilter{@Overridepublicvoiddestroy(){//TODOAuto-generatedmethodstub}@OverridepublicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)throwsIOException,ServletException{request.setCharacterEncoding(utf-8);PrintWriterout=null;try{//使用我们自定义的响应包装器来包装原始的ServletResponseResponseWrapperwrapper=newResponseWrapper((HttpServletResponse)response);chain.doFilter(request,wrapper);Stringresult=wrapper.getResult();result=getDealContent(result,request);response.setContentLength(-1);out=response.getWriter();//输出最终的结果out.write(result);}catch(Exceptione){e.printStackTrace();}finally{try{if(out!=null){out.flush();out.close();}}catch(Exceptione){e.printStackTrace();}}}/**对返回的html进行处理*/publicStringgetDealContent(Stringresult,ServletRequestrequest){//对html进行处理后返回处理结果returnresult;}}