1

I'm attempting to fix a character encoding issue. I realize this is really not a good way to go about it but currently I am just going to bandage it up and when character encoding comes up in a new to do list I will bring a proper solution.

anyway currently i've fixed a character encoding issue with french characters by doing this in the action:

String folderName = request.getParameter(PK_FOLDER_NAME);
if (response.getCharacterEncoding().equals("ISO-8859-1") && folderName != null) {
            folderName = URLDecoder.decode(new String(folderName.getBytes("ISO-8859-1"), "UTF-8"), "UTF-8");
        }

however what is the string is an array? how would i do it? for example what if string is as such:

String[] memos = request.getParameterValues(PK_MEMO);

how would i convert using the URLDecoder than?

MBMJ
  • 5,323
  • 8
  • 32
  • 51
OakvilleWork
  • 2,377
  • 5
  • 25
  • 37

2 Answers2

2

thanks guys...

the answer I was looking for was this (which works):

if (response.getCharacterEncoding().equals("ISO-8859-1") && memos != null) {
        for(int n=0; n< memos.length; n++) {
            memos[n] = URLDecoder.decode(new String(memos[n].getBytes("ISO-8859-1"), "UTF-8"), "UTF-8");
        }   
    }
OakvilleWork
  • 2,377
  • 5
  • 25
  • 37
1

You're going about it completely the wrong way.

You're first obtaining the request parameter (and thus it start to get parsed which makes it too late to set the proper encoding for request parameter parsing!) and you're determing the encoding of the response instead of the request. This makes no sense.

Just set the request encoding before ever getting the first parameter. It will then be used during parsing the request parameters for the first time.

request.setCharacterEncoding("UTF-8");
String folderName = request.getParameter(PK_FOLDER_NAME);
String[] memos = request.getParameterValues(PK_MEMO);
// ...

Note that you'd normally like to call request.setCharacterEncoding("UTF-8") in a servlet filter so that you don't need to repeat it over all servlets of your webapp.

The response encoding is normally to be configured on the JSP side by @page pageEncoding on a per-JSP basis or <page-encoding> in web.xml on an application-wide basis.

Don't try to introduce bandages/workarounds, it would only make things worse. Just do it the right way from the beginning on.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • It's hard to nail down the problem without knowing details about the environment, its configuration and the exact problem symptoms. At least, reading the article behind the "See also" link should bring a lot of insight. – BalusC Jul 25 '12 at 14:22