How to handle string encoding in java?

I was really discouraged by the java string encoding. It has a lot of automatic conversions. and I can't find a regular one. Anyone have a good idea? for example: In the jsp page it has a link like this

http://localhost:8080/helloworld/hello?world=ๅ‡นใ„‰

      

And then we need to handle it, so we do this:

String a = new String(request.getParameter("world").toString().getBytes("ISO-8859-1"), 
                      "UTF-8");
a = "http://localhost/" + a;

      

And when I debug it I found what is correct.

And then I pass this to the session object: request.getSession (). setAttribute ("hello", a);

Later on the jsp page with "Big5" encoding and I am trying to get the attribute and display, And I found that the "ๅ‡น ใ„‰" characters are corrupted.

How can I solve this?

+2


source to share


2 answers


This is not how you convert between character sets. What you need to worry about is this part:

 request.getParameter("world").toString().getBytes("ISO-8859-1")

      

Once you have a string as a string, it will be stored inside 16-bit unicode. Getting it as bytes and then telling java how to treat those bytes as if they were UTF-8 does nothing good.



If you find that everything is in order, it's just a coincidence. As soon as you call this getParameter ("world"). ToString () you have a Unicode string. Further decoding and encoding will just break certain characters, it just won't break yours.

The question is, how do you get this attribute to display later? You are saying that the encoding of the jsp page is not unicode, but rather Big5, so what do you do to take that string out of the attribute map and put it on this page? This is the likely source of the problem. Given the misunderstanding about how to handle character conversion when getting a parameter, it's likely there are bugs on this Big5 page as well.

By the way, do you really need to use Big5? Does UTF-16 (if not UTF-8) work? This can, of course, eliminate some of the headaches.

+12


source


The following code will work



String a = new String(request.getParameter("world").toString().getBytes("ISO-8859-1"), 
                      "UTF-16");

      

-1


source







All Articles