chairbender
chairbender

Reputation: 849

Chinese UTF-8 characters are appearing incorrectly in Weblogic 10.3 but not Tomcat 6

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?

Upvotes: 1

Views: 8366

Answers (2)

chairbender
chairbender

Reputation: 849

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

Upvotes: 1

Joop Eggen
Joop Eggen

Reputation: 109567

Maybe this helps

<?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app>
    <jsp-descriptor>
        <encoding>UTF-8</encoding>
    </jsp-descriptor>
    <charset-params>
        <input-charset>
            <resource-path>/*</resource-path>
            <java-charset-name>UTF-8</java-charset-name>
        </input-charset>
    </charset-params>
</weblogic-web-app>

Upvotes: 0

Related Questions