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

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

问题描述

最近我实现了基于) 通常表示负责的数据传输本身非常了解源和目标中使用的编码.在一般的 Web 应用程序中,只有 2 个地方是这种情况:数据通过 JDBC 传输到/从 DB 的点和数据通过 response.getWriter( 传输到 HTTP 响应的点))(由 JSP 隐式使用).

在您使用属性文件的特定情况下,没有数据库的方法,因此 HTTP 响应仍然是主要的嫌疑人.当服务器没有被指示使用 UTF-8 解码写入 HTTP 响应的字符时,可能会发生此问题,而是使用一些平台默认编码,最常见的是 ISO-8859-1.这样,源中的任何字符都没有被 ISO-8859 覆盖-1 会变成问号.由于 ISO-8859-1 专门用于拉丁字符,因此这将影响所有非拉丁字符,例如中文、日语、阿拉伯语、西里尔字母、希伯来语、梵语等.他们都会写成问号.

这可以在每个 JSP 的基础上通过将以下内容添加到 JSP 的最顶部来修复:

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

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

或者,更好的是,通过将以下条目添加到 webapp 的 web.xml 在应用程序范围的基础上修复它:

<jsp-property-group><url-pattern>*.jsp</url-pattern><页面编码>UTF-8</页面编码></jsp-property-group></jsp-config>

另见:

Recently I have implemented internationalization based on How to internationalize a Java web application?.

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?

解决方案

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).

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.

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

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

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

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>

See also:

这篇关于servlet 应用程序中的 Unicode 字符显示为问号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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