File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes How to compress jsp? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How to compress jsp?" Watch "How to compress jsp?" New topic

How to compress jsp?

Michael Deng

Joined: Dec 05, 2000
Posts: 12
Hi All,
In servlet, we can compress the output to speed up the web page loading.
In JSP, how can we do this?
Michael Deng

Example in Servlets:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
GZIPOutputStream gzos =
new GZIPOutputStream(response.getOutputStream());
} catch(IOException ie) {
// handle the error here
} // end doGet
Kyle Brown
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
Well, it's probably easiest to install a zip utility (like modgzip for Apache) on your web server so that all of your content is zipped regardless of its origin...
You could do what you're doing in Servlets in a JSP, too, but the other option is probably far easier.

Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at for other WebSphere information.

Joined: Jan 20, 2002
Posts: 17
I used this code in my program. but when i used GZipOutputStream. It displayed the download box on the client machine.
Plz help me how to send the fast output to the client.
Thanks in advance,
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Michael,
I think Kyle suggestion is the best. However if you don't/can't use Apache as a web front end, have a look at your web/application server console to see if your can enable this behaviour.
If not, depending on the version of servlet engine you're using, you might be able to use a Filter servlet to do the compression. This provides better encapsulation of the feature.
Anyway, before performing gzip compression, you must ensure the client web browser supports the gzip compression.
Try to find the word gzip in the Accept-Encoding line in the request headers. If found, the client accepts gzip.
In that case, you can compress your output using the GZIPOutputStream, but do not forget to tell the client you are sending him a compressed stream by setting gzip in the Content-Encoding line of the server's response header.
Michael Deng

Joined: Dec 05, 2000
Posts: 12
Thanks a lot for your guys' reply.
Do you know where can I download modgzip? Is it free?
I tried to search it in google, but, it seems I counldn't download it anymore.
I have another question, if my jsps contain lots of gif, will modgzip put all the images in one zip file at the same time?
I agree. Here's the link:
subject: How to compress jsp?
It's not a secret anymore!