This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Servlets and the fly likes Compressed servlet output with GZIP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Compressed servlet output with GZIP" Watch "Compressed servlet output with GZIP" New topic
Author

Compressed servlet output with GZIP

Susan Barretta
Greenhorn

Joined: Aug 30, 2001
Posts: 5
Hi everyone,
I've seen a million places on the web that post a solution to compressing servlet output. Basically the solution encapsulates the HttpServletResponse OutputStream in a GZIPOutputStream. Some solutions encapsulate the GZIPOutputStream in a PrintWriter - very handy so that you can do write() with a string, eliminating the step of converting the string to a byte array.
I've been able to get a little standalone program to generate an output file via PrintWriter encapsulating GZIPOutputStream and then I can read the file back in again using GZIPInputStream.
But when I do this in my servlet code, the input stream just shows up empty back at the client - I get "unexpected end of ZLIB input stream".
I am not running this in a browser, by the way, the client is a standalone application.
Any suggestions for why the InputStream turns up empty? And are there any straightforward ways to monitor what is getting written to that output stream on the server side?
Thanks.
Vikas Aggarwal
Ranch Hand

Joined: Jun 22, 2001
Posts: 140
Try to first run this program.. I have tested it on IE as well as on netscape.
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import java.util.zip.*;
public class EncodedPage extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out;
String encodings = request.getHeader("Accept-Encoding");
String encodeFlag = request.getParameter("encoding");
String title;
if ((encodings != null) &&
(encodings.indexOf("gzip") != -1) &&
!"none".equals(encodeFlag)) {
title = "Page Encoded with GZip";
OutputStream out1 = response.getOutputStream();
out = new PrintWriter(new GZIPOutputStream(out1), false);
response.setHeader("Content-Encoding", "gzip");
} else {
title = "Unencoded Page";
out = response.getWriter();
}
out.println( "<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1 ALIGN=CENTER>" + title + "</H1>\n");
String line = "Blah, blah, blah, blah, blah. " +
"Yadda, yadda, yadda, yadda.";
out.println(""+line+"</BODY></HTML>");
out.close();
}
}


Vikas Aggarwal

Founder @
Leads and Deals Limited

www.LeadsAndDeals.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compressed servlet output with GZIP