I have written jsp pag开发者_如何学Goe 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.
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.
精彩评论