中文UTF-8字符在Weblogic 10.3中显示不正确,但不是Tomcat 6 [英] Chinese UTF-8 characters are appearing incorrectly in Weblogic 10.3 but not Tomcat 6

查看:200
本文介绍了中文UTF-8字符在Weblogic 10.3中显示不正确,但不是Tomcat 6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用Java EE和Spring的网站,需要输出中文UTF-8字符。我有一个servlet执行request.getRequestDispatcher(...)。转发(请求,响应)一些处理后的jsp。在这个servlet中,在执行转发之前,我有

  response.setCharacterEncoding(UTF-8); 
response.setContentType(text / html; charset = UTF-8);

在jsp文件(和项目中的每个jsp文件)的顶部, / p>

 <%@ page language =javapageEncoding =UTF-8contentType =text / html; charset = UTF -8%>作为测试,我有一个中文的UTF-8字符硬编码在该jsp页面上。当我将此应用程序部署到Tomcat 6并打开servlet时,浏览器检测到该页面是UTF-8并输出中文字符:采用
当我部署到Weblogic 10并打开servlet时,浏览器检测页面是UTF-8,但显示:éÂ



在我的weblogic启动脚本,我尝试了不同的java选项-Dfile.encoding,如UTF-8和utf8



在weblogic.xml中,我有:

 < charset-params> 
< input-charset>
< resource-path> / *< / resource-path>
< java-charset-name> UTF-8< / java-charset-name>
< / input-charset>
< / charset-params>

< jsp-descriptor>
< encoding> UTF-8< / encoding>
< / jsp-descriptor>

在web.xml中,我有:

  filter> 
< filter-name> encodingFilter< / filter-name>
< filter-class> org.springframework.web.filter.CharacterEncodingFilter< / filter-class>
< init-param>
< param-name> encoding< / param-name>
< param-value> UTF-8< / param-value>
< / init-param>
< init-param>
< param-name> forceEncoding< / param-name>
< param-value> true< / param-value>
< / init-param>
< / filter>

< filter-mapping>
< filter-name> encodingFilter< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>
...
< jsp-config>
< jsp-property-group>
< url-pattern> *。jsp< / url-pattern>
< page-encoding> UTF-8< / page-encoding>
< / jsp-property-group>
< / jsp-config>

但这些都没有解决问题。



所有的源文件都是UTF-8编码的。



此外,当我有一个.jsp或.html页面有一个汉字,直接而不是由Weblogic中的servlet转发它,汉字正确显示。



此外,在我的servlet中,我甚至不执行转发到jsp,而直接写入汉字到响应(使用response.getWriter 。



为什么它可以在Tomcat 6中工作但不是Weblogic 10?

解决方案

虽然我不知道为什么写入字符到响应直接导致汉字显示不正确,我想出了什么是导致字符不正确时转发到一个jsp页面。我转发的jsp页面做一个c:import另一个jsp页面。 Tomcat和Weblogic以不同的方式处理。为了确保导入的文件具有正确的编码,我需要在c:import标记中指定属性characterEncoding =UTF-8,并导致导入的标记以正确的编码输出。似乎默认字符编码不同的应用程序服务器之间的任何代码c:import依赖(这可能解释为什么直接从servlet响应中的字符工作正如我想在Tomcat,而不是Weblogic)。


I'm developing a website using Java EE and Spring that needs to output Chinese UTF-8 characters. I have a servlet which performs a request.getRequestDispatcher(...).forward(request,response) to a jsp after some processing. In this servlet, before doing the forward, I have

response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");

At the top of the jsp file (and every jsp file in my project), I have:

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

As a test, I have a Chinese UTF-8 character hardcoded on that jsp page. When I deploy this application to Tomcat 6 and hit the servlet, the browser detects the page is UTF-8 and outputs the Chinese character: 采 When I deploy this to Weblogic 10 and hit the servlet, the browser detects the page is UTF-8 but displays: éÂÂ

In my weblogic startup script, I have tried different java options for -Dfile.encoding, such as UTF-8 and utf8, but the Chinese character was garbled regardless of the setting.

In weblogic.xml I have this:

<charset-params>
    <input-charset>
        <resource-path>/*</resource-path>
        <java-charset-name>UTF-8</java-charset-name>
    </input-charset>
</charset-params>

<jsp-descriptor>
    <encoding>UTF-8</encoding>
</jsp-descriptor>

In web.xml I have this:

filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter> 

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

But these have not solved the problem.

All of my source files are UTF-8 encoded.

Furthermore, when I have a .jsp or .html page with a Chinese character in it, and access it directly instead of being forwarded to it by the servlet in Weblogic, the Chinese character displays correctly.

Also, when, in my servlet, I don't even perform the forward to the jsp and instead write Chinese characters directly to the response (using response.getWriter().write()), the Chinese character is still incorrect (even though the browser correctly determines that the encoding is UTF-8).

Why does it work in Tomcat 6 but not Weblogic 10? How do I get the character to display?

解决方案

Although I'm not sure why writing characters to the response directly caused chinese characters to be displayed incorrectly, I've figured out what was causing characters to be incorrect when forwarding to a jsp page. The jsp page I forward to does a c:import of another jsp page. Tomcat and Weblogic handle this differently. In order to ensure the imported file has the correct encoding, I needed to specify the attribute "characterEncoding="UTF-8"" in the c:import tag, and that caused the imported tag to be output with the correct encoding. It seems that the default character encoding varies between app servers for whatever code c:import relies on (and that probably explains why writing characters directly from the servlet response was working as I wanted in Tomcat but not Weblogic).

这篇关于中文UTF-8字符在Weblogic 10.3中显示不正确,但不是Tomcat 6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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