Dario Andrade wrote:

> Can anyone recommend me of a good Java programming  mailing list, I dont
> want to clutter the list with non related JSP questions...
>
>         The question is: How can I understand a little bit more about charsets? I
> seem to be confused about how it converts characters to numerical values.
>
>     char c = 'c';
>     int n = (int) c;
>
>     String str = "The numerical value is: " + n;
>
>         It works fine when tested in a simple main class. But when I grab content
> from Javamail (Content-type is "text/plain; charset=ISO-8859-1"), the result
> is a disaster.
>
>         How do I make sure I am using the correct charset, or even, how do I
> convert charsets to make sure I am working with right char codes?
>

Welcome to the wonderful world of internationalization :-).  While this is sorta
offtopic, it is an issue that many JSP and servlet developers need to understand
better, so it deserves at least a brief answer here.

Basically, the key thing to remember is that Java uses Unicode internally for its
character strings.  That means that you need to convert *incoming* textual data into
Unicode if it is not already, and convert *outgoing* textual data from Unicode to the
desired character encoding on the response.

One of the main reasons that the Reader and Writer family of classes were added to
JDK 1.1 were to deal with this very issue.  To convert an input file encoded in the
ISO-8859-1 character set, for example, you can say something like this:

    FileReader reader =
      new FileReader("myfile", "ISO-8859-1");

and the reader will perform the appropriate transformations (from ISO-8859-1 to
Unicode) for you.  There are corresponding capabilities to create a Writer with any
character encoding supported by the JDK.

In a JSP/servlet environment, the character set used on a request or response is
supposed to be included in the "Content-Type" HTTP header.  Then, you can call
request.getCharacterEncoding() to tell what encoding the input is using.  On the
output side, you might say:

    response.setContentType("text/html", "ISO-8859-1");

Once you've done this, the Reader you get back from request.getReader(), or the
writer you get back from response.getWriter(), will be configured to do the correct
transformations.

There is a lot more information about internationalization available.  One place to
start is to download the JDK documentation bundle for your JDK version (if you
haven't done so already), and read the section on Internationalization.

>
> Thanks,
> Dario Andrade

Craig McClanahan

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to