Jetty Response with no Charset

2019-06-26 01:04发布

问题:

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.

Does anyone know how to do this?

I've tried intercepting the Response and setting the CharacterEncoding to null or "" but that gives Exceptions.

I am using Jetty 6.1.6.

回答1:

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). I compared it to tomcat (4.1.29, old too). 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:

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

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

Servlet code:

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

=>
tomcat: text/plain;charset=ISO-8859-1
jetty: text/plain

Servlet code:

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

=>
tomcat: 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.



回答2:

I think that this not a matter of which servlet container you use, but what you do with the response inside your 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). But be sure to call the methods named above before calling getWriter, otherwise setting the encoding will have no effect!



回答3:

The charset is very useful info. 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).