Utf8 and jsp trying to figure out what's 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>

Prints the following:

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

What I miss here. Why oe3 works, but not oe1 or oe2. Maybe something is happening here with an encoding that I don't understand.

+3
source share
2 answers

Add this to the top of the JSP so that it prints characters using UTF-8 and allows the browser to interpret the response as UTF-8.

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

The tag <meta>does not. Moreover, it is ignored when the page is transmitted via HTTP.

See also:

+4
source

All Articles