jsp utf encoding

后端 未结 4 1369
广开言路
广开言路 2021-01-18 00:54

I\'m having a hard time figuring out how to handle this problem:

I\'m developing a web tool for an Italian university, and I have to display words with accents (such

相关标签:
4条回答
  • 2021-01-18 01:12

    In the jsp page directive you should try setting your content-type to utf-8, which will set the pageEncoding to utf-8 also.

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

    UTF-8 is not default content type in jsp, and there are all sorts of interesting problems that arise from this. The problem is that the underlying stream is interpreted as an ISO-8859-1 stream by default. If you write some unicode bytes to this stream, they will be interpreted as ISO-8859-1. I find that setting the encoding to utf-8 is the best solution.

    Edit: Furthermore, a string variable in java should always be unicode. So you should always be able to say

    System.out.println(myString) 
    

    and see the proper character set coming in the console window of your web-server (or just stop in the debugger and examine it). I suspect that you'll be seeing incorrect characters when you do this, which leads me to believe you have an encoding problem when constructing the string.

    0 讨论(0)
  • 2021-01-18 01:22

    I have some international jsp's [which have "special" international (with respect to English) characters].

    Inserting this [and only this, i.e: no contentType directive also (that made a duplicate contentType error)] at the top of them got them to save and render correctly:

    <%@page pageEncoding="UTF-8"%>
    

    This reference [http://www.inter-locale.com/codeset1.jsp] helped me discover that.

    0 讨论(0)
  • 2021-01-18 01:22
    String s = parseText(filename, position)
    

    Where is this method defined? I'm guessing that it's your own method, which opens the file and extracts a particular chunk of the data. Somewhere in this process it's getting converted from bytes to characters, probably using the default encoding for your JVM.

    If the default encoding of your running JVM doesn't match the actual encoding in the file, you're going to get incorrect characters in your string. Added to that, if you're reading content that is encoded in a multi-byte form (such as UTF-8), your "position" may point into the middle of a multi-byte encoding.

    If the source files are in well-formed XML, you'll be much better off using a real parser (such as the one built into the JDK) to parse them, since the parser will provide the correct translation of bytes to characters. Then use an XPath expression to retrieve the values.

    If you haven't used an XML parser in the past, here are two documents that I wrote on parsing and XPath.


    Edit: one thing that you may find helpful is to print out the actual character values in the string, using something like the following:

    public static void main(String[] argv) throws Exception
    {
        String s = "testing\u20ac";
        for (int ii = 0 ; ii < s.length() ; ii++)
        {
            System.out.println(ii + ": " + (int)s.charAt(ii) + " = " + s.charAt(ii));
        }
    }
    

    You should probably also print out your default character set, so that you know how any particular sequence of bytes is translated to characters:

    public static void main(String[] argv) throws Exception
    {
        System.out.println(Charset.defaultCharset());
    }
    

    And finally, you should examine the served page as raw bytes, to see exactly what's being returned to the client.


    Edit #2: the character ò is Unicode value 00F2, which would be UTF-8 encoded as C3 B2. These two codes doesn't correspond to the symbols that you showed in your earlier answer.

    For more on Unicode characters, see the code charts at Unicode.org.

    0 讨论(0)
  • 2021-01-18 01:24

    I had also the same problem, everything is "utf-8" and why i see
    senseless characters and the problem was in jsp and it must be at the head of the page.

     <%request.setCharacterEncoding("utf-8");%>
    

    and everything will be ok.

    0 讨论(0)
提交回复
热议问题