servlet应用程序中的Unicode字符显示为问号 [英] Unicode characters in servlet application are shown as question marks

查看:70
本文介绍了servlet应用程序中的Unicode字符显示为问号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我已经基于>如何对Java Web应用程序进行国际化实现了国际化? .

但是现在我在某些页面上遇到了问题.如果是英语,则显示效果很好,但是如果选择其他语言,则该页面中的所有值都显示为?????????.

But now I am facing a problem in some of the pages. If it is English language, it is showing well, but if we choose any other language then all the values in that page are showing as ?????????.

我已经检查了服务器日志中的异常,但是找不到任何人.

I have checked for exceptions in server logs, but I couldn't find anyone.

这是怎么引起的,我该如何解决?

How is this caused and how can I solve it?

推荐答案

看到??????而不是可理解的字符(甚至代替 Mojibake )通常表示负责数据传输本身非常了解源和目标中使用的编码.在普通的Web应用程序中,只有两种情况:通过JDBC将数据传输到DB或从DB传输数据到数据点,以及通过response.getWriter()将数据传输到HTTP响应的时间(隐式使用).通过JSP).

Seeing ?????? instead of intelligible characters (and even instead of Mojibake) usually indicates that the data transfer responsible is by itself very well aware about the encoding used in both the source and the destination. In the average web application there are only 2 places where this is the case: the point when the data is transferred to/from the DB by JDBC and the point when the data is transferred to the HTTP response by response.getWriter() (as implicitly used by JSP).

在特殊情况下使用属性文件时,没有数据库的手段,因此HTTP响应仍然是主要的可疑对象.如果未指示服务器使用UTF-8解码正在写入HTTP响应的字符,而是使用某些平台默认编码(最常见的是ISO-8859-1),则会发生此问题.这样,源中未被ISO-8859- 覆盖的任何字符 1将变成问号.由于ISO-8859-1专用于拉丁字符,因此这会影响所有所有非拉丁字符,例如中文,日语,阿拉伯语,西里尔字母,希伯来语,希伯来语,梵语等.它们都将被写为问号.

In your particular case with properties files, there's no means of a DB, so the HTTP response remains as the main suspect. This problem can happen when the server isn't been instructed to use UTF-8 to decode the characters which are being written to the HTTP response, but instead some platform default encoding, most commonly ISO-8859-1. This way any character in the source which is not covered by ISO-8859-1 will be turned into a question mark. As ISO-8859-1 is exclusively dedicated to Latin characters, this will thus affect all non-Latin characters such as Chinese, Japanese, Arabic, Cyrillic, Hebrew, Sanskrit, etcetera. They would all be written as question marks.

可以在每个JSP的基础上,通过在JSP的最顶部添加以下内容来解决此问题:

This can be fixed on a per-JSP basis by adding the following to the very top of JSP:

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

(请注意,您确实需要将此文件放入每个 JSP以及包含文件!)

(note that you really need to put this in every JSP, also the include files!)

或者更好的方法是,通过在webapp的web.xml中添加以下条目来在整个应用程序范围内对其进行修复:

Or, better, fix it on an application-wide basis by adding the following entry to webapp's web.xml:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>

另请参见:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆