简体   繁体   English

没有Charset的Jetty响应

[英]Jetty Response with no Charset

I'm using Jetty to test a webservice we have and I am trying to get it to respond with no charset under the content-type header. 我正在使用Jetty来测试我们拥有的Web服务,并且我试图让它在content-type标头下没有charset响应。

Does anyone know how to do this? 有谁知道如何做到这一点?

I've tried intercepting the Response and setting the CharacterEncoding to null or "" but that gives Exceptions. 我已经尝试拦截响应并将CharacterEncoding设置为null或“”但是这会给出异常。

I am using Jetty 6.1.6. 我正在使用Jetty 6.1.6。

I think that this not a matter of which servlet container you use, but what you do with the response inside your servlet. 我认为这不是你使用哪个servlet容器的问题,而是你在servlet中对响应做了什么。 If you set your character encoding by calling ServletResponse's setContentType (2.3) or setCharacterEncoding (2.4, 2.5) with parameter null or "" it should work (didn't try myself). 如果你通过调用ServletResponse的setContentType(2.3)或setCharacterEncoding(2.4,2.5)来设置你的字符编码参数为null""它应该工作(没试过自己)。 But be sure to call the methods named above before calling getWriter, otherwise setting the encoding will have no effect ! 但是调用getWriter 之前一定要调用上面提到的方法,否则设置编码将无效

I tried it my self now, but I must admit, my jetty is a very old one (4.2., but does everything the way I need it). 我现在尝试了自己,但是我必须承认,我的码头是一个非常古老的码头(4.2。但是按照我需要的方式完成所有事情)。 I compared it to tomcat (4.1.29, old too). 我把它比作tomcat(4.1.29,也是旧的)。 I checked the content type with the following code: 我使用以下代码检查了内容类型:

URL tomcatUrl = new URL("http://localhost:18080/ppi/jobperform/headertest")//tomcat;
URLConnection tconnect = tomcatUrl.openConnection();
System.out.println("tomcat: " + tconnect.getContentType());


URL jettyUrl = new URL("http://localhost:13818/ppi/jobperform/headertest")//jetty;
URLConnection jconnect = jettyUrl.openConnection();
System.out.println("jetty: " + jconnect.getContentType());

And the result was as follows: 结果如下:

Servlet code: Servlet代码:

    response.setContentType("");
    response.getWriter().write("Return");

=> =>
tomcat: ;charset=ISO-8859-1 tomcat :; charset = ISO-8859-1
jetty: 码头:

Servlet code: Servlet代码:

     response.setContentType("text/plain");
     response.getWriter().write("Return");

=> =>
tomcat: text/plain;charset=ISO-8859-1 tomcat:text / plain; charset = ISO-8859-1
jetty: text/plain 码头:文字/平原

Servlet code: Servlet代码:

response.setContentType("text/plain;charset=UTF-8");
response.getWriter().write("Return");

=> =>
tomcat: text/plain;charset=UTF-8 tomcat:text / plain; charset = UTF-8
jetty: text/plain;charset=UTF-8 jetty:text / plain; charset = UTF-8

So it looks as if the older jetty does exactly what you want, while tomcat does what you got from the newer jetty. 所以它看起来好像旧的码头完全符合你的要求,而tomcat则是你从新的码头做的。

The charset is very useful info. charset是非常有用的信息。 Instead of trying to force a good product (Jetty) to do the wrong thing I would rather try to teach the consumer of the service to do the right thing (recognize and honor charset). 而不是试图强迫一个好的产品(Jetty)做错事我宁愿尝试教服消费者做正确的事情(识别并尊重charset)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM