We help IT Professionals succeed at work.

Unicode String with format &#code; and format \ucode

ndhai
ndhai asked
on
Medium Priority
530 Views
Last Modified: 2010-08-05
Hello
I would like to know how Java process a String unicode
- with format &#code;
- with format \ucode
there are any difference ?

In a test1.jsp file, I have 2 String unicode
String str1 = "\u041D\u0435\u043F\u0440\u0430\u0432\u0438\u043B\u044C\u043D\u044B\u0439 \u043A\u043E\u0434 \u0434\u043E\u0441\u0442\u0443\u043F\u0430!"

String str2 = "Неправильный код доступа!"

(Normally, There are a same value "Неправильный код доступа!" in Russe)

in test1.jsp, I forward to test2.jsp :

<jsp:forward page="test2.jsp" >
      <jsp:param name="str1" value="<%= str1%>" />
      <jsp:param name="str2" value="<%= str2%>" />
</jsp:forward>


at test2.jsp, I get 2 para str1 and str2 and display them.
Only str2 is displayed well. The str1 is displayed "???????????? ??? ???????!"

I have to use a unicode String \ucode but I dont know how to fix this problem ?
thanks a lot
best regards
ndhai
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
>>&#1072;

These are decimal entities

Author

Commented:
I dont know why the Russe Text doesn't display well
=>>> There a a same value "&#1053;&#1077;&#1087;&#1088;&#1072;&#1074;&#1080;&#1083;&#1100;&#1085;&#1099;&#1081; &#1082;&#1086;&#1076; &#1076;&#1086;&#1089;&#1090;&#1091;&#1087;&#1072;!"
CERTIFIED EXPERT
Top Expert 2016

Commented:
A font that can display te characters must be installed

Author

Commented:
@CEHJ  : I dont understand what you mean ?
Someone can tell me how can I display Russe text well on this site ?
thanks
CERTIFIED EXPERT
Top Expert 2016

Commented:
You need to set the response encoding to UTF-8 and the viewing browser must have a font that can display Russian characters
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
try passing the string as a request atribute instead of a request parameter

Author

Commented:
@CEHJ : I ready set the response encoding to UTF-8 (If not, how the str2 &#code; is displayed well):
  - in test1.jsp : response.setContentType("text/html,utf-8");
  - in test2.jsp : <%@ page contentType = "text/html;charset=utf-8" %>

@objects : do you mean that
- I set request.setAttribute("sMsg", sMsg); in test1.jsp
- I get (String)request.getAttribute("str1"); in test2.jsp

I dont want to do that because if I do that, then I have to change a lot of Java codes (I dont want to touch (modify) the code.

thanks a lot
ndhai
CERTIFIED EXPERT
Top Expert 2016

Commented:
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> - I set request.setAttribute("sMsg", sMsg); in test1.jsp
> - I get (String)request.getAttribute("str1"); in test2.jsp

yes, but should be:

- I set request.setAttribute("sMsg", sMsg); in test1.jsp
- I get (String)request.getAttribute("sMsg"); in test2.jsp

> I dont want to do that because if I do that, then I have to change a lot of Java codes (I dont want to touch (modify) the code.

you're going to have to change something :)
worth trying at least to see if it fixes it

Author

Commented:
sorry I typed wrong. I mean str1 in 2 codes lines!
yes, It works :)

but you are sure we haven't another solution ?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> but you are sure we haven't another solution ?

another would be to convert the string. Something like:

     <jsp:param name="str2" value="<%= Utils.encode(str2) %>" />
CERTIFIED EXPERT
Top Expert 2016

Commented:
What does

request.getCharacterEncoding()

in test2.jsp print?

Author

Commented:
@objects : I dont understand what you mean here Utils.encode ? It is a function what concerts \ucode to &#code; or a standart function of Java ?

@CEHJ: printed 'null' !
Is there some wrong here ? I ready set <%@ page contentType = "text/html;charset=utf-8" %> on the top of test2.jsp
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
that was just an example if you wanted to write your own conversion utility

passing it as a request attribute is a better way to pass it regardless imo, and code changes are minimal.

Author

Commented:
if I add the code request.setCharacterEncoding("utf-8");, it prints "utf-8" but str2 is always displayed ?????!
CERTIFIED EXPERT
Top Expert 2016
Commented:
In that case, try

request.setCharacterEncoding("UTF-8") ;

before using the request

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
nothing is changed! it displays ????
code request.getCharacterEncoding()  prints 'UTF-8'

in test2.jsp I ready have :
- on the top : <%@ page contentType = "text/html;charset=UTF-8" %>
- fisrt code Java : request.setCharacterEncoding("UTF-8");
- in meta tag HTML: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>


Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
that all looks fine.  why don't you want to pass it as an attrbute?  passing as parameter is more intended for parameters being passed in externally.

Author

Commented:
@objects: If we haven't another solution, then I will use set and getAttribute.
but I hope there is another one!
CERTIFIED EXPERT
Top Expert 2016

Commented:
Just as a matter of interest, try

String s = request.getParameter("x"); // (Whatever)
out.println(new String(s.getBytes(), "UTF-8"));

Author

Commented:
it printed "??????" :(

CEHJ:  do you think there is some thing wrong in my codes ? or this is process way of Java for unicode String ? could you test it your self ?
thanks in advance
ndhai
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>CEHJ:  do you think there is some thing wrong in my codes ?

At the moment i'd guess not. Hardcode that String in test2.jsp and tell me what you see

Author

Commented:
If I set str2 = "\u041D\u0435\u043F\u0440\u0430\u0432\u0438\u043B\u044C\u043D\u044B\u0439 \u043A\u043E\u0434 \u0434\u043E\u0441\u0442\u0443\u043F\u0430!";
and print it
>> output like that : http://nguyenduchai.free.fr/temp/msgRusse.JPG

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
yes, thats what I'd expect, its displaying it fine.
Its the passing as a parameter thats corrupting it.

Top Expert 2004

Commented:
Hi ndhai,

The main difference between the 2 encodings is :
   \u<hexa>        is only understood by Java which translate it directly to the corresponding character, which may not be understood by your web browser, unless changing character encoding to unicode (UTF-8, UTF-16, ...)

   &#<deci>;
   &#x<hexa>;       are only understood by your HTML browser.
CERTIFIED EXPERT
Top Expert 2016

Commented:
*Are* you  passing the parameter as entities rather than Unicode escaped ndhai?

Author

Commented:
@CEHJ : I have to using the unicode string like that (\ucode, not &#code;). In fact, this value is gotten from a Ressource file (BundleRessource)!
CERTIFIED EXPERT
Top Expert 2016

Commented:
Just call this in a loop then use it as the parameter:


      public static String toHexEntity(char c) {
            StringBuilder sb = new StringBuilder(7);
            return sb.append("&#x").append(Integer.toHexString(c)).append(';')
                        .toString();
      }
CERTIFIED EXPERT
Top Expert 2016

Commented:
public static String toHexEntities(char[] chars) {
            StringBuffer sb = new StringBuffer(chars.length * 8);
            for (int i = 0; i < chars.length; i++) {
                  sb.append(toHexEntity(chars[i]));
            }
            return sb.toString();
}

Author

Commented:
@CEHJ: I have a question, just a doubt
when you talk
"In that case, try
request.setCharacterEncoding("UTF-8") ;
before using the request"

it is test1.jsp or test2.jsp or both of them ?

thanks
CERTIFIED EXPERT
Top Expert 2016

Commented:
It won't harm to do it in both unless you have something exotic coming into test1

Author

Commented:
the key of response : we have to set request.setCharacterEncoding("UTF-8") ; before using it!
thanks CEHJ and everybody
regards
ndhai
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.