views:

51

answers:

4

Hi,

I java code, i am having a string name = "örebro"; // its a swedish character.

But when i use this name in web application. i prints some specail character at 'Ö' character.

Is there anyway i can use the same character as it is in "örebro".

i did some thing like this but does not worked.

String name = "örebro";
byte[] utf8s = name .getBytes("UTF-8"); 
name = new String(utf8s, "UTF-8");

But the name at the end prints the same, something like this. �rebo

Please guide me

+3  A: 

You need to set the encoding of your output to UTF8.

SLaks
+3  A: 

The Java code you've provided is pointless, it will do nothing. Java Strings are already perfectly capable of encoding any character (though you have to be careful with literals in the source code, as they depend on the encoding the compiler uses, which is platform-dependant).

Most likely your problem is that your webpage does not declare the encoding correctly in the HTTP header or the HTML meta tags.

Michael Borgwardt
A: 

It is likely the browser that reads the page does not know the encoding.

  • send the header (before any other output) something in Java like
    ServletResponse resource; (...)
    resource.setContentType ("text/html;charset=utf-8");

  • in your html page, mention the encoding by sending (printing)
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

ring0
A: 

If the page used to generate the output is jsp it's useful to precise

<%@ page contentType="text/html; charset=utf-8" %>
Xavier Combelle