博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用HttpServletResponseWrapper获取渲染jsp以后的html
阅读量:4213 次
发布时间:2019-05-26

本文共 1310 字,大约阅读时间需要 4 分钟。

   有些场景,我们会试图获取渲染jsp以后的html,或者修改一下生成json,例如把普通的json换成跨域的jsonp。

ResponseFacade只提供了getOutStream(),但是获取不了stream的容器bytearray,不通过hacker的方式根本获取不了。Tomcat也意识到这一点,提供了HttpServletResponseWrapper帮我们解决这个问题,对于HttpServletResponseWrapper有这样的comments:

 

       Provides a convenient implementation of the HttpServletResponse interface

 * that can be subclassed by developers wishing to adapt the response from a

 * Servlet. This class implements the Wrapper or Decorator pattern. Methods

 * default to calling through to the wrapped response object.

 

我们只要在其子类添加一些逻辑就能得到我们想要的东西,用byteArrayOutSteam换掉coyoteOutStream,再通过toByteArray就能获取到html,但这得借助filter帮我们用responsewrapper在dochain()换掉reponse,

上代码吧

/**       * Creates a GenericResponseWrapper       */     public GenericResponseWrapper(final HttpServletResponse response, final OutputStream outstr) {          super(response);          this.outstr = new FilterServletOutputStream(outstr);      } /**       * Gets the outputstream.       */     public ServletOutputStream getOutputStream() {          return outstr;      }    /**       * Gets the print writer.       */     public PrintWriter getWriter() throws IOException {          if (writer == null) {              writer = new PrintWriter(new OutputStreamWriter(outstr, getCharacterEncoding()), true);          }          return writer;      }
 

 

转载地址:http://jmdmi.baihongyu.com/

你可能感兴趣的文章
mongodb group 有条件的过滤组合个数。
查看>>
关于mongodb的 数组分组 array group
查看>>
MongoDB新的数据统计框架介绍
查看>>
mongodb 增加全文检索索引
查看>>
mysql数据库主从同步的问题解决方法
查看>>
QC数据库表结构
查看>>
测试工具厂商的编程语言什么时候“退休”?
查看>>
资源监控工具 - Hyperic HQ
查看>>
LoadRunner中Concurrent与Simultaneous的区别
查看>>
SiteScope - Agentless监控
查看>>
质量度量分析与测试技术 培训大纲
查看>>
欢迎加入【亿能测试快讯】邮件列表!
查看>>
为什么我们的自动化测试“要”这么难
查看>>
LoadRunner性能脚本开发实战训练
查看>>
测试之途,前途?钱途?图何?
查看>>
adb常用命令
查看>>
通过LR监控Linux服务器性能
查看>>
通过FTP服务的winsockes录制脚本
查看>>
LRwinsocket协议测试AAA服务器
查看>>
Net远程管理实验
查看>>