Skip to content
Advertisement

Servlet encoding woes in Open Liberty

I have a simple test servlet that should output a non ASCII character (right single quotation mark – ’). In Tomcat, it works, but in Liberty I get junk. Is this a bug in Liberty, am I doing it wrong, or a config issue?

JavaScript

and the web.xml

JavaScript

From Tomcat the response is (courtesy of Fiddler):

JavaScript

The body hex is: E2, 80, 99 (which is correct UTF-8 for ’)

From Liberty it is

JavaScript

The hex for that content is: C3, A2, E2, 82, AC, E2, 84, A2

Dev tools (F12) matches Fiddler.

I’ve tried moving around the code

JavaScript

before and after the getWriter (the docs say it should be before getWriter). With and without setCharacterEncoding and all kinds of things, content types etc.

The .java file itself is saved with UTF-8 encoding.

It’s curious that the content length header says 3 bytes with either server, but with Liberty the actual content length is 8 bytes. As if the bytes have been re-encoded?

So, what is going on here?

UPDATE: taking out the out.close() per @pmdinh’s answer had an effect, but didn’t fix it. This is the closest I could get to proper behaviour

JavaScript

This correctly encodes it, but now the content length is wrong by 2 bytes. So the response is

JavaScript

but since the content-length is 2 short the browser shows ’12

Also note that the placing of setCharacterEncoding and setContentType matters and other combinations make the output even worse (incorrect encoding).

Advertisement

Answer

Remove the

JavaScript

that should resolve the issue.

Ref: https://www.ibm.com/support/pages/apar/PM71666

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement