[JEE] JSP Encoding (Solved)

I am writing a web application to fetch HTML codes from another web page, extract useful data and display it on JSP. As the source web page contains characters in Big5 encoding, I put the following tag in JSP:
  1. <meta http-equiv="Content-Type" content="text/html; charset=Big5" />
複製代碼
However, the browser still display JSP in ISO-8859-1 encoding (Western). I then add the following code in JSP:
  1. <%@page contentType="text/html;charset=big5"%>
複製代碼
This time, the browser tries to display JSP in Big5 encoding, but the characters still cannot be read.



I also tried to put the JSP page directive tag only, but it doesn't work (just like the above image).

Is it due to the encoding of Java compiler? Or other reasons?

Please help and thank you.

[ 本帖最後由 GraphiteCube 於 2009-6-25 22:56 編輯 ]

first  make sure the encoding format of data  is correct
second  try this
<%@ page contentType="text/html; charset=big5"%>

or using  utf-8 encoding

[ 本帖最後由 xeon0541 於 2009-6-25 17:40 編輯 ]

TOP

回覆 2# 的帖子

I added it into JSP but it doesn't help.

Character encoding is annoying...

TOP

seem encoding format of data is incorrect  , you need to converting the encoding format of  data ,for example:
String rs="你我";
String col_1_b=rs getBytes("8859_1");
String a=new String(col_1_b,"big5");

[ 本帖最後由 xeon0541 於 2009-6-25 20:12 編輯 ]

TOP

原帖由 xeon0541 於 2009-6-25 20:11 發表
seem encoding format of data is incorrect  , you need to converting the encoding format of  data ,for example:
String rs="你我";
String col_1_b=rs getBytes("8859_1");
String a=new String(col_1_b,"big ...

唔使咁做架....

TOP

just simple example

TOP

You said the source page is using Big5, as all string and character in Java are in unicode, so u need to either
- convert the source page content to unicode 1st by assigning correct encoding when you are reading text and store as String
or
- use byte[] all the way.
Otherwise the character encoding will be a mess.
If you are using String, u can use either unicode or big5 for pageEncoding attribute in <%page %> tag
or if you are using byte[], you may need to print it out directly using response.

TOP

回覆 5# 的帖子

Could you suggest any solutions?

TOP

回覆 7# 的帖子

If I want to convert the fetched page content from Big5 encoding to UTF-8 encoding, could getBytes() method in class String help?

Thanks.

TOP

回覆 9# 的帖子

You don't need to use getBytes(), just set the correct encoding when fetching from source, e.g. InputStreamReader(InputStream in, String charsetName)
will do.
Then you will get the content in Unicode(Java internal encoding), then you just need to specify the output encoding using pageEncoding attribute in <%page%>, JspWriter will do all the convertion for you.

TOP