JSP gzip output stream
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?
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
精彩评论