Issue
I am having trouble in returning compressed response (GZip) from my Java Servlet, to a JSP.
Flow :
- Request comes to java servlet
- Process the request, and create a JSON object, with the response
- Convert the JSON object to string
- Compress the response string with GZip
- The compressed response string is set as attribute in the request object and control passed to JSP
- In the JSP, the response string (compressed) is printed on screen
Precautions :
- Request object has "Accepting-Encoding" set with "gzip"
- Response header has "Content-Encoding" set to "gzip"
- Response content type is set to "application/json"
- Response character encoding is set to "ISO-8859-1"
Result :
- Firefox shows "Content Encoding Error"
- Chrome shows "Error 330 (net::ERR_CONTENT_DECODING_FAILED): Unknown error."
Can anyone help point me out, in the right direction please?
Solution
The compressed response string is set as attribute in the request object and control passed to JSP
You shouldn't have forwarded a JSON response to a JSP. You should have printed the JSON plain to the response and have the JavaScript/Ajax code in your JSP Android app to call the URL of the servlet which returns the JSON. See also How to use Servlets and Ajax?.
As to the GZIP compression, you shouldn't do it yourself. Let the server do itself.
Fix your code to remove all manual attempts to compress the response, it should end up to basically look like this:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String json = createItSomehow();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
}
That's all, if you let your Android app call the URL of the servlet, it'll retrieve the JSON string.
Finally edit the server configuration to turn on automatic GZIP compression. In case of for example Tomcat, that would be a matter of adding compression="on"
to the <Connector>
element in Tomcat's /conf/server.xml
file:
<Connector ... compression="on">
As per the documentation, the compressable mime types defaults to text/html,text/xml,text/plain
. You can configure this to add application/json
.
<Connector ... compression="on" compressableMimeType="text/html,text/xml,text/plain,application/json">
Unrelated to the concrete problem, the response character encoding must be set to UTF-8
which is as per the JSON specification.
Answered By - BalusC