JSP gzip output stream

て烟熏妆下的殇ゞ 提交于 2019-12-13 15:01:36

问题


I know I can gzip the output stream by using something like..


OutputStream outA = response.getOutputStream();
outWriter = new PrintWriter(new GZIPOutputStream(outA), false);      
response.setHeader("Content-Encoding", "gzip");
outWriter.println(.....);
outWriter.close();

in a JSP, but is it possible to write it as:

OutputStream outA = response.getOutputStream();
outWriter = new PrintWriter(new GZIPOutputStream(outA), false);      
response.setHeader("Content-Encoding", "gzip");
%>
...

I know this is done in PHP for example by capturing the output buffer before it is flushed, gzipping the buffer, and then finally writing it.

But is it possible in a JSP?


回答1:


This Java code doesn't belong in a JSP.

If your intent is to gzip the HTML code generated by JSP, then you need to configure it at appserver level. In JBoss (and Tomcat) you need to set the compression attribute of the <Connector> element in /server.xml to on.

<Connector compression="on">

That's all. It'll be by default applied on all text/* responses (HTML/CSS/JS).

See also:

  • Tomcat 6.0 HTTP Connector configuration reference
  • Web application performance tips and tricks


来源:https://stackoverflow.com/questions/4827004/jsp-gzip-output-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!