my dog learned polymorphism*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to compress jsp?" Watch "How to compress jsp?" New topic
Author

How to compress jsp?

Michael Deng
Greenhorn

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?
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

Joined: Aug 10, 2001
Posts: 3892
    
    5
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


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
RAJESH RASTOGI
Greenhorn

Joined: Jan 20, 2002
Posts: 17
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

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.
Cheers
Michael Deng
Greenhorn

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?
Cheers!
Michael
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to compress jsp?
 
Similar Threads
JSP with large data loads slow in Solaris10 OS , we are using Jrun4 and nested logic:iterate
Getting contents form the Response Object
Question: How do you send a JSP page as compressed GZIP encoding?
Compressing Jsp
zipping of class files/jsp