468,245 Members | 1,688 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,245 developers. It's quick & easy.

receiving compressed data

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
1 5378
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.

Similar topics

8 posts views Thread by Dennis Hotson | last post: by
reply views Thread by Norman Barker | last post: by
reply views Thread by Vladimir Nesterovsky | last post: by
13 posts views Thread by Leonardo Francalanci | last post: by
2 posts views Thread by Author | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.