jsp utf encoding

后端 未结 4 1398
广开言路
广开言路 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条回答
  •  Happy的楠姐
    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.

提交回复
热议问题