0

When I write the following

é

or even

<%=new String(new byte[]{(byte) 0xC3, (byte) 0xA9}, "UTF-8")%>

I get a string with the length of 1. It contains a single UTF-8 character C3A9 or é.

But when it gets written to the browser, the browser cannot decode it unless I use latin1. So that means it is getting encoded and written as western even though in the top of the JSP I have

<%@page import="..." contentType="text/html" pageEncoding="UTF-8" session="false"%>

How can I get the JSP to output UTF-8 instead of writing a UTF-8 header and then encoding with latin1? "Western (ISO-8859-1)"

4
  • Wow, what's wrong with just writing é or very maybe at highest <%="é"%> in the template? Commented May 2, 2011 at 19:59
  • @BalusC: I wanted to guarantee that there were no editor encoding issues. Edited. Commented May 2, 2011 at 20:01
  • Ah, you're using MSIE as browser? Commented May 2, 2011 at 20:21
  • @BalusC: No. Firefox and Chrome. Commented May 2, 2011 at 20:38

1 Answer 1

2

You should only use pageEncoding, and write the symbol itself, rather then referring to it by its code.

Also make sure your .jsp file is also encoded in UTF-8. Your IDE should handle the pageEncoding attribute and set the encoding appropriately, but with particular settings or if not using an IDE, the .jsp can still be encoded in ISO-8859-1. Change that to UTF-8

Finally, make sure your response is really using UTF-8 as encoding (use firebug for example). If not, try setting it response.setCharacterEncoding(..)

Sign up to request clarification or add additional context in comments.

9 Comments

Replaced the <%=...%> clause with single character é. I am using NetBeans IDE and have verified with a hex editor that it correctly encoded in UTF-8 and shows the two bytes C3A9 on the left and the character é on the right. Still it does not work. I even checked the generated ..._jsp.java file. It correctly shows the same character and same encoding. So the problem can only be in either the .java to .class compile time or the encoding at runtime.
@George Bailey well, it works for me. Try my last suggestion. If it doesn't work, there's something more in your case that I'm not seeing.
Thus you were already writing to the response before you called @page pageEncoding? Otherwise this shouldn't happen and this is actually also not the correct solution for in a JSP. The @page should be the firstmost line in the JSP and the preprocessing servlet -if any- shouldn't have written any character to the response body.
@BalusC: I think the problem was that I was using include instead of forward in order to call the JSP. I have not been using forward because it changes the URL to the path to the JSP which is never what I am after.
the <%@page pageEncoding="UTF-8"%> has to be set in ALL JSPs, thus also in the parent JSP and not only in the child JSP.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.