Top.Mail.Ru
March 1st, 2004 - Java developers — LiveJournal
? ?

Java developers

March 1st, 2004
Image

12:57 pm - Imagebanana - It's all Greek to me

I have a <%= expr %> tag in a JSP page that inserts text that's come from a database. If the text is in English all is well. When the text is Chinese, garbage comes out.

In the servlet response API there's a method for getting the character encoding, but no method for setting it. Instead you can set the response locale, which according to the Javadoc includes "setting the headers (including the Content-Type's charset) as appropriate." So I could set the response locale to Chinese, but that would mean that other languages stopped displaying corectly.

What I really want is to set the character encoding to UTF-8, then (assuming that I set the appropriate response header) it's the browser's job to display the text correctly. But as I can only set the locale, what locale will get me a UTF-8 encoding? Or is there some other way of controlling character-to-byte conversion in the response?

As an aside for those who are still following this, I've noticed that it I tell the browser to interpret the page as UTF-8, the Chinese comes out right (or at least, as I can't read Chinese, it at least looks like Chinese). However, doing this makes French words with accented letters have Chinese characters in the middle. 8~(

 

01:23 pm - Imagecyb3rj - instanceof

i understand the very basic gist of 'instanceof'. however, there are details i don't understand. to dive straight to my situation, consider the following code:
try {
  asYouMight();
} catch (Exception e) {
  if (e instanceof ArrayOutOfBoundsException) {
    doSomethingVeryDifferent();
  } else {
    throw new fit(e);
  }
}

so, it appears to my relatively novice eyes that instanceof can test an instance of an object for ANY subtype, right?

i kinda don't get it, and i want to. does 'e' still have all of the "ArrayOutOfBoundException-specific" fields and methods, or does it lose them because it was "caught" as an "Exception"? i know AOOBE doesn't have much in the way of separate fields and methods from a normal Exception, but other classes may -- perhaps this code better illustrates:
public void aMethod( SuperClass a ) {

  if (a instanceof SuperClass) {
    a.methodOnSuperClass();
  } else if (a instanceof SubClass) {
    a.methodOnSubClass();
  }
}

would that work?
Image

05:55 pm - Imagebanana - JSP UTF-8 solution

I think I've fixed the problem from my previous post. The solution is there in the Servlet API docs, but it's not where you might think. Under ServletResponse.setContentType it says "The given content type may include a character encoding specification, for example, text/html;charset=UTF-8."

It seems that not only is this used to set the Content-Type HTTP header, but also for specifying a character converter for the servlet to use.

I say "seems" because my only evidence for this is that I now have English, Chinese and French accented characters on the same page and they all look right to me. I haven't actually figured out what the UTF-8 values should be and checked that the page source containes them...
Powered by LiveJournal.com
Image