• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to compress jsp?

 
Michael Deng
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
In servlet, we can compress the output to speed up the web page loading.
In JSP, how can we do this?
Thanks!
Michael Deng

Example in Servlets:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
...
try {
GZIPOutputStream gzos =
new GZIPOutputStream(response.getOutputStream());
gzos.write(sb.toString().getBytes());
gzos.close();
} catch(IOException ie) {
// handle the error here
...
}
...
} // end doGet
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
RAJESH RASTOGI
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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,
Rajesh
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Cheers
 
Michael Deng
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Cheers!
Michael
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic