更改Glassfish Servlet编码/字符集

时间:2017-06-19 23:54:33

标签: servlets character-encoding glassfish

我将一个Serializable对象发送到Glassfish 4.1服务器上的Servlet,当我尝试记录我得到的阿拉伯语值时,该对象有字符串字段和阿拉伯字符(?????)

我已阅读ThisThis以及其他许多帖子,但没有解决问题,我在Tomcat 8服务器上尝试过相同的代码,它运行正常

我应该如何使用Glassfish服务器或我的Servlet正确读取UTF-8字符?

发送Serializable对象:

        url = new URL(ServerInfo.STORABLE_RECEIVER_URL);            
        http = (HttpURLConnection) url.openConnection();
        http.setRequestProperty("content-type", "application/x-object;  charset=utf-8");
        http.setRequestProperty("Accept-Charset", "UTF-8");
        http.setDoOutput(true);
        http.setDoInput(true);          
        out = new ObjectOutputStream(http.getOutputStream());
        out.writeObject(t);

的web.xml

<filter>
    <filter-name>Set Response Character Encoding</filter-name>
    <filter-class>net.abdullahcodes.serv.MyFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>Set Response Character Encoding</filter-name>
    <url-pattern>/TradableReceiver</url-pattern>
    <url-pattern>/*</url-pattern>
    <url-pattern>/</url-pattern>
</filter-mapping>

<locale-encoding-mapping-list>
    <locale-encoding-mapping>
        <locale>ar</locale>
        <encoding>UTF-8</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
        <locale>en</locale>
        <encoding>UTF-8</encoding>
    </locale-encoding-mapping>
</locale-encoding-mapping-list>

MyFilter

This

的Omri Spector回复

的glassfish-web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
    <jsp-config>
    </jsp-config>
    <parameter-encoding default-charset="UTF-8" />
</glassfish-web-app>

Servlet测试收到Serializable对象:

        log("sys: "+System.getProperty("file.encoding"));
        log("def: "+Charset.defaultCharset());
        log("size: "+b.totalRowsCount()+", "+b.totalColumnsCount());
        log("test arabic: هلوووووووووويااا");
        System.out.println("test sysout: اوووه خطبها نصيب");
        while (b.hasNext()) {
            Position p = new Position();
            p.setEn(b.next());
            p.setAr(b.next());
            log("p: "+p.toString());                
        }

Glassfish上的控制台输出:

log():net.abdullahcodes.serv.TradableReceiver: sys: Cp1252
log():net.abdullahcodes.serv.TradableReceiver: def: windows-1252
log():net.abdullahcodes.serv.TradableReceiver: size: 4, 2
log():net.abdullahcodes.serv.TradableReceiver: test arabic: ????????????????
Info: test sysout: ????? ????? ????
log():net.abdullahcodes.serv.TradableReceiver: p: id: 0, en: fatima, ar: abod
log():net.abdullahcodes.serv.TradableReceiver: p: id: 0, en: maryam, ar: nora
log():net.abdullahcodes.serv.TradableReceiver: p: id: 0, en: ????, ar: ????
log():net.abdullahcodes.serv.TradableReceiver: p: id: 0, en: ????, ar: ????

Tomcat上的控制台输出:

log INFO: net.abdullahcodes.serv.TradableReceiver: sys: UTF-8
log INFO: net.abdullahcodes.serv.TradableReceiver: def: UTF-8
log INFO: net.abdullahcodes.serv.TradableReceiver: size: 4, 2
log INFO: net.abdullahcodes.serv.TradableReceiver: test arabic: هلوووووووووويااا
test sysout: اوووه خطبها نصيب
log INFO: net.abdullahcodes.serv.TradableReceiver: p: id: 0, en: fatima, ar: abod
log INFO: net.abdullahcodes.serv.TradableReceiver: p: id: 0, en: maryam, ar: nora
log INFO: net.abdullahcodes.serv.TradableReceiver: p: id: 0, en: احمد, ar: صالح
log INFO: net.abdullahcodes.serv.TradableReceiver: p: id: 0, en: حسين, ar: 

1 个答案:

答案 0 :(得分:3)

您需要设置命令行属性:

 -Dfile.encoding=UTF-8

启动Glassfish时。

如果您从IDE启动,则需要将此设置添加到启动配置中。

如果Glassfish作为服务启动,那么您需要设置一个环境变量:

 JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8