Re: Beating my head -- JSPs and encoding

Discussion in 'Java' started by Hegemony Cricket, Jul 31, 2003.

  1. (Hegemony Cricket) wrote in message news:<>...
    > I'm using JSPs to generate some pages that may show either Chinese or
    > English characters.
    >
    > My JSP looks essentially like this:
    >
    > <%@ page import="..." contentType="text/html;charset=UTF-8"%>
    > <%
    > String some_passed_in_text = (String) request.getAttribute( "blah"
    > );
    > %>
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    > <html>
    > <head>
    > <meta http-equiv="content-type" content="text/html;
    > charset=UTF-8">
    > </head>
    > <body>
    > ...
    > <%= some_passed_in_text %>
    > ...
    > </body>
    > </html>
    >
    >
    > The text to be shown, which is either English or Chinese (and is UTF-8
    > encoded), is retrieved in the some_passed_in_text variable, and
    > inserted
    > into the document.
    >
    > The problem is that this JSP gets compiled into
    >
    > ...
    > out.print( some_passed_in_text );
    > ...
    >
    > and "out" is an instance of JspWriter, which seems to only write in
    > the "platform's default character encoding" (according to the
    > JspWriter
    > API doc), not the UTF-8 I want to be written. Hence, whenever I pass
    > in Chinese text, it shows in the browser as question marks.
    >
    > Is there a way to change the encoding that "out" uses? If not, is
    > there some other way I can do this?



    Of course, after crunching on this problem for two days, I figured
    it out minutes after posting here.

    The JSP in question was actually one of several that were included
    together to generate a single page. I had only done the

    <%@ page contentType="text/html;charset=UTF-8"%>

    on the topmost JSP, which generates a

    response.setContentType("text/html;charset=UTF-8");

    in the compiled JSP. The later JSPs, having not specified the
    contentType, by default generate this call in their compiled state:

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

    This overrides the previous call, so the browser tries to show the
    page with the default encoding. Once I changed all the JSPs that
    comprised the page to the correct content type, the page rendered
    correctly.

    Of course, I didn't give you all enough information to see the
    problem, so you're all off the hook. ;-)

    I was misled by the javadoc for JspWriter, which reads

    public abstract void print(java.lang.String s)
    throws java.io.IOException

    Print a string. If the argument is null then the string
    "null" is printed. Otherwise, the string's characters are
    converted into bytes according to the platform's default
    character encoding, and these bytes are written in exactly
    the manner of the Writer.write(int) method.

    They are not converted according the the platform's default
    character encoding at all, it seems, but rather according to
    what you specify. Lesson learned.

    Thanks for your consideration...


    --Mark
    Hegemony Cricket, Jul 31, 2003
    #1
    1. Advertising

  2. Hegemony Cricket

    umaj

    Joined:
    Aug 22, 2012
    Messages:
    1
    Thank you very much for providing details. Your post helped me in showing UTF8 characters properly when JspWriter.out used with custom tags (the main JSP had one included JSP file).
    umaj, Aug 22, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. showme

    PHP and ASP.NET go HEAD to HEAD

    showme, Jul 8, 2004, in forum: ASP .Net
    Replies:
    13
    Views:
    702
    Sherif ElMetainy
    Jul 10, 2004
  2. Brian Andrus

    Learning and beating my head

    Brian Andrus, May 4, 2004, in forum: Java
    Replies:
    2
    Views:
    344
    Erwin Moller
    May 4, 2004
  3. Jonathan Smith
    Replies:
    0
    Views:
    249
    Jonathan Smith
    Jun 6, 2006
  4. Replies:
    1
    Views:
    317
    Gianni Mariani
    Feb 11, 2006
  5. J Haas
    Replies:
    232
    Views:
    1,819
    Eleanor McHugh
    Jun 4, 2009
Loading...

Share This Page