receiving compressed data

S

stroumf

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
 
J

Jonas Raoni

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 :]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top