testing123
testing123

Reputation: 11457

UTF8 and jsp, trying to understand what is going on

I created the following jsp:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
<%
byte[] oe1 = {-61,-123};
byte[] oe2 = {-123,-61};
byte[] oe3 = "œ".getBytes("UTF-8");
%>
byte[] oe1 = {-61,-123}: '<%=new String(oe1, "UTF-8")%>'<br/>
byte[] oe2 = {-123,-61}: '<%=new String(oe2, "UTF-8")%>'<br/>
byte[] oe3 = "œ".getBytes("UTF-8"): '<%=new String(oe3, "UTF-8")%>'<br/>
oe3[0], oe3[1]: <%=oe3[0]%>, <%=oe3[1]%>
    </body>
</html>

Which prints out the following:

byte[] oe1 = {-61,-123}: '�'
byte[] oe2 = {-123,-61}: '??'
byte[] oe3 = "œ".getBytes("UTF-8"): 'œ'
oe3[0], oe3[1]: -61, -123 

What am I missing here. Why does oe3 work but not oe1 or oe2. There is probably something going on here with encoding that I do not understand

Upvotes: 3

Views: 305

Answers (2)

leonbloy
leonbloy

Reputation: 75986

To add to BalusC answer, the jsp encoding can also be set globally in web.xml file: http://bordet.blogspot.com/2007/09/jsp-page-encoding.html

Upvotes: 2

BalusC
BalusC

Reputation: 1109262

Add this to the top of the JSP to let it print characters using UTF-8 and let the browser interpret the response as UTF-8.

<%@ page pageEncoding="UTF-8" %>

The <meta> tag doesn't do that. Even more, it's ignored when the page is served over HTTP.

See also:

Upvotes: 4

Related Questions