By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,730 Members | 1,500 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,730 IT Pros & Developers. It's quick & easy.

receiving compressed data

P: n/a
Hi,

Simple question, I want to receive compressed data from a server using
AJAX.
At the client I make an xmlHttpRequest. Next I want to set the
accept-header to Gzip, but I keep getting errors on that under opera.
At server-side I first check the accept-header, if it supports gzip I
make a ZIP-stream and set the content-encoding to gzip. Normaly I would
think the browser itself can handle it and give the uncompressed
response.

Clientcode (javascript):
xmlHttp = =new XMLHttpRequest();
xmlHttp.onreadystatechange = stateChange;
//don't work
xmlHttp.setRequestHeader("Accept-Encoding", "gzip");
xmlHttp.open("GET", "servlet", false);
xmlHttp.send(null);

function stateChange() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
var response = xmlHttp.responseText;
alert(response);
var uncompressedResponse = response;
document.getElementById("code").innerHTML =
"<p>"+uncompressedResponse+"</p>";
}
}

Servercode (servlet):
public void doGet(HttpServletRequest request, HttpServletResponse
response) {
String text = "sometext";
response.setContentType("text/html");
OutputStream out = null;
String encoding = request.getHeader("Accept-Encoding");
if (encoding != null && encoding.indexOf("gzip") != -1)
{
response.setHeader("Content-Encoding" , "gzip");
out = new GZIPOutputStream(response.getOutputStream());
}
else if (encoding != null && encoding.indexOf("compress") != -1)
{
response.setHeader("Content-Encoding" , "compress");
out = new ZipOutputStream(response.getOutputStream());
}
else
{
out = response.getOutputStream();
System.out.println("gewone uitvoerstroom");
}
byte[] b = text.getBytes();
out.write(b);
}

What am I doing wrong?

thx in advance

Nov 21 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
stroumf escreveu:
At the client I make an xmlHttpRequest. Next I want to set the
accept-header to Gzip, but I keep getting errors on that under opera.
At server-side I first check the accept-header, if it supports gzip I
make a ZIP-stream and set the content-encoding to gzip. Normaly I would
think the browser itself can handle it and give the uncompressed
response.
Well, here it's working fine on Opera. My web server compression is
turned on and for the XHR request I just outputed the received headers,
the gzip was there and as I could read, the text was decompressed by the
browser.
Servercode (servlet):
If you think the problem is within your Java code, post it in a Java
newsgroup, you'll get better answers :]
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Nov 22 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.