有些场景,我们会试图获取渲染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;
}
分享到:
相关推荐
源代码 博文链接:https://msj.iteye.com/blog/179663
javax/servlet/jsp/resources/jsp_2_0.xsd javax/servlet/jsp/resources/jsp_2_1.xsd javax/servlet/jsp/resources/jspxml.xsd javax/servlet/LocalStrings.properties javax.servlet....
所以在JSP容器通过Response获取输出流之前,前面的流并没有关闭,所以会造成该异常的报出。 本文解决办法: 在前面所说的网络解决办法中,使用的是一种躲避的方式解决该问题,也就是置之不理。 这里提供一个...
Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. -------------------------------------------------------------------------------- G ...
HttpServletResponseWrapper HttpSession HttpSessionActivationListener HttpSessionAttributeListener HttpSessionBindingEvent HttpSessionBindingListener HttpSessionContext HttpSessionEvent ...
javax.servlet.http.HttpServletResponseWrapper javax.servlet.http.HttpSessionBindingListener javax.servlet.http.HttpSessionAttributeListener javax.servlet.http.HttpSessionActivationListener