-1

I have written jsp page in which i create String with gb2312 charset, Then i pass the string to the JSP Page. But in the jsp page i have give pageencoding and meta tag as UTF-8. I am expecting that the characters have to be displayed as junk one and after i change character encoding in browser as gb2312 it has to be displayed correctly. But what actually happen was in UTf-8 itself it displayed as correct one. But when i change that to gb2312 Character encoding it has been displayed as junk one. Please Help. What am i doing wrong. Help to correct me.

Roshan
  • 2,019
  • 8
  • 36
  • 56

1 Answers1

1

A String doesn't have an encoding. It is just an array of chars, and each char has a universal unicode value.

It's only when the chars must be transformed into bytes, to be saved in a file or streamed to a browser, that the encoding is used to transform chars into bytes.

Since you said in your JSP's page encoding directive that it had to use UTF-8, your string has been encoded as UTF-8. The browser knows that it's UTF-8 thanks to a response header, and thus transforms the bytes it receives from the server into chars correctly.

If you tell the browser to ignore the encoding set as a response header, and use gb2312, the browser will try to interpret the bytes as gb2312, and since it's UTF-8, it will display incorrect chars.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • then how to Stream the content to the jsp page. i dont have idea abt it. please help. – Roshan Oct 10 '11 at 12:04
  • I have used the following code to print the stream using BufferedReader. int bb; while ( ( bb = br.read() ) != -1 ) { char c = (char)bb; out.print(c); } But using this also results in the same as i mentioned above in question. – Roshan Oct 10 '11 at 12:38
  • You don't have to stream anything. The web container streams the characters generated by the JSP to the browser. Your code is fine as is, since the browser displays what it has to display. Why would you change anything? – JB Nizet Oct 10 '11 at 12:40
  • In out.print it prints the character correctly in UTF-8. But when i change to gb2312 it displays as junk one. – Roshan Oct 10 '11 at 12:43
  • What haven't you understood in my answer? Why do you want to change the browser encoding to gb2312, since the HTML page is encoded in UTF-8, and the browser displays everything correctly? – JB Nizet Oct 10 '11 at 12:51
  • Yes i Understand. But my problem is to use incorrect input in the jsp file and while changing the character encoding it has to be corrected. Do u know any way to implement this. Please help. Or the way am i implementing my problem description is wrong? – Roshan Oct 10 '11 at 13:01
  • Are you saying that you would like to generate HTML encoded in an incorrect way (i.e. set the encoding to UTF-8 in the response header, but actually generate gb2312 encoded HTML)? You want to produce a buggy answer? This won't be possible with JSPs (and it's a good thing it's not possible: I don't know why anyone would want that). – JB Nizet Oct 10 '11 at 13:06
  • Actually i want to handle the input of those encoded with wrong charset.While Changing the charset in browser it reflects the correct one. there are cases where wrongly encoded stream may come. How to input rawinput to browser without encoding . This was my actual problem. – Roshan Oct 10 '11 at 13:20