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

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

问题描述

我最近根据)通常表示负责的数据传输本身非常清楚在源和目标中使用的编码。在平均Web应用程序中,只有两个位置,在这种情况下:数据通过JDBC传输到数据库的点和数据通过响应传输到HTTP响应的点.getWriter()(由JSP隐含使用)。



在你的特定情况下,没有DB的方法, HTTP响应。当服务器没有被指示使用UTF-8来解码被写入HTTP响应的字符,而是一些平台默认编码,最常见的是ISO-8859-1时,这可能发生。这样,源中的任何字符(不是由ISO-8859-1 覆盖)会变成一个问号。由于ISO-8859-1专用于拉丁字符,因此这将影响所有非拉丁字符,例如中文,日语,阿拉伯语,西里尔语,希伯来语,梵语等。



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

 <%@ page pageEncoding =UTF-8%> 

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



或者,更好的是,通过添加以下条目到webapp的 web.xml

 < jsp-config& 
< jsp-property-group>
< url-pattern> *。jsp< / url-pattern>
< page-encoding> UTF-8< / page-encoding>
< / 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, there's no means of a DB, so left over the HTTP response. This 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天全站免登陆