无法压缩发送到JSP的Java服务器响应 [英] Cannot Compress Java Server Response Sent To JSP

查看:195
本文介绍了无法压缩发送到JSP的Java服务器响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从我的Java Servlet返回压缩响应(GZip)到JSP时遇到问题。

I am having trouble in returning compressed response (GZip) from my Java Servlet, to a JSP.

流程:


  1. 请求来到java servlet

  2. 处理请求,创建一个JSON对象,响应

  3. 将JSON对象转换为字符串

  4. 使用GZip压缩响应字符串

  5. 压缩响应字符串设置为请求对象中的属性,传递给JSP

  6. 在JSP中,响应字符串(压缩)打印在屏幕上

  1. Request comes to java servlet
  2. Process the request, and create a JSON object, with the response
  3. Convert the JSON object to string
  4. Compress the response string with GZip
  5. The compressed response string is set as attribute in the request object and control passed to JSP
  6. In the JSP, the response string (compressed) is printed on screen

注意事项:


  1. 请求对象的Accepting-Encoding设置为gzip

  2. 响应

  3. 响应内容类型设置为application / json

  4. 设置了响应字符编码至ISO-8859-1

  1. Request object has "Accepting-Encoding" set with "gzip"
  2. Response header has "Content-Encoding" set to "gzip"
  3. Response content type is set to "application/json"
  4. Response character encoding is set to "ISO-8859-1"

结果:


  1. Firefox显示内容编码错误

  2. Chrome显示错误330(net :: ERR_CONTENT_DECODING_FAILED):未知错误。

任何人都可以帮助我指出正确的方向。

Can anyone help point me out, in the right direction please?

推荐答案


压缩的响应字符串在请求对象中设置为属性,控制权传递给JSP

您不应该将JSON响应转发到JSP。您应该将JSON纯文字打印到响应中,并在JSP Android应用中使用 JavaScript / Ajax代码调用返回JSON的servlet的网址。另请参见如何使用Servlet和Ajax?

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?.

至于GZIP压缩,你不应该自己做。让服务器自己做。

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);
}

最后编辑服务器配置以启用自动GZIP压缩。在例如Tomcat的情况下,这将是 compression =on < Connector> server.xml file:

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:

根据文档,可压缩mime类型默认为 text / html,text / xml,text / plain 。您可以将此配置为添加 application / json

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






无关具体问题,响应字符编码必须设置为 UTF-8 ,这是根据JSON规范。


Unrelated to the concrete problem, the response character encoding must be set to UTF-8 which is as per the JSON specification.

这篇关于无法压缩发送到JSP的Java服务器响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆