在Webapp中使用自定义字符集 [英] Use custom charset in webapp

查看:53
本文介绍了在Webapp中使用自定义字符集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用来自Web应用程序的自定义字符集时遇到了一些麻烦.该字符集在JAR中提供,并且 Charset.forName("MYCUSTOMCHARSET")可以正常运行,而Java SE应用程序不会出现问题.但是,从Web应用程序中,此方法将引发 UnsupportedCharsetException .

I'm having some trouble using a custom charset from a webapp. The charset is provided in a JAR and Charset.forName("MYCUSTOMCHARSET") works without problems from a Java SE application. However from within a webapp this method will throw UnsupportedCharsetException.

我知道这是类加载器的问题,并且我知道可以通过将JAR添加到servlet引擎的类路径中来解决此问题.但这不是我想要的.我希望能够向客户交付一个自包含的WAR文件,而客户不必摆弄周围的容器.

I understand that this is an issue of class loaders and I know that I can solve the problem by adding the JAR to the servlet engine's classpath. But that is not what I want. I want to be able to deliver a self-contained WAR file to my customer without the customer having to fiddle with the surrounding container.

换句话说:我正在寻找一种方法,该方法将允许我的Web应用程序手动"加载字符集.从提供自定义字符集的JAR的 services 文件夹中,我可以看到CharsetProvider的全限定名称,例如 com.acme.CustomCharsetProvider .我想知道这是否会对我有帮助?

In other words: I'm searching for a method that will allow my webapp to load the charset "manually". From the services folder in the JAR which provides the custom charset I can see the fully qualified name of the CharsetProvider, e.g. com.acme.CustomCharsetProvider. I wonder if this will help me somehow ?

平台:Java 8和Tomcat 8.

Platform: Java 8 and Tomcat 8.

推荐答案

基于以上Joop的评论,我提出了以下建议.下面的方法是一种防弹配方的尝试,无论我们处于什么情况下(例如独立Java SE应用程序,webapp等),该方法始终能够掌握自定义字符集.

Based on comment above from Joop I was able to come up with the following. The method below is an attempt at a bullet-proof recipe for always being able to get hold of the custom charset no matter what scenario we are in (e.g. standalone Java SE application, webapp, etc).

在该方法中,预期自定义字符集将由类 com.acme.CustomCharsetProvider 提供(用您自己的字符集代替).

In the method the custom charset is expected to be provided by class com.acme.CustomCharsetProvider (replace with your own).

该方法使用三种不同的尝试来获取字符集:

The method uses three different attempts to get hold of the charset:

  1. 标准方法.这将在Java SE应用程序中起作用,但在webapps,除非拥有定制字符集的JAR可用servlet引擎的类路径上.
  2. ServiceLoader方法.应该有效,但就我而言,这不可行.无法解释原因.
  3. 手动实例化提供程序类.(这对我有用)
  1. Standard method. This will work in Java SE applications but not in webapps unless the JAR which holds the custom charset is available on the servlet engine's classpath.
  2. ServiceLoader method. This should work, but not in my case. Can't explain why.
  3. Manually instantiating the provider class. (this works for me)

代码如下:

public static Charset getMyCustomCharset() throws java.nio.charset.UnsupportedCharsetException {
    Charset customCharset = null;
    try {
        // This will fail if running in web container because
        // the JDK loads charsets using the system class loader in the servlet
        // engine (e.g. Tomcat) so unless the JAR is available on the engine's
        // classpath then the charset will not be visible to the webapp.
        // The solution is to load the charset "manually" as below.
        customCharset = Charset.forName(CHARSET_NAME);
    } catch (Exception ex) {
        // Try to load the charset manually using ServiceLoader concept
        for (CharsetProvider charsetProvider : ServiceLoader.load(com.acme.CustomCharsetProvider.class)) {
            customCharset  = charsetProvider.charsetForName(CHARSET_NAME);
            if (customCharset != null) {
                break;
            }
        }
        // Make a final attempt. This time directly, i.e. without the use of
        // the ServiceLoader.
        if (customCharset == null) {
            com.acme.CustomCharsetProvider p = new com.acme.CustomCharsetProvider();
            customCharset = p.charsetForName(CHARSET_NAME);
        }
    }
    if (customCharset == null) {
        throw new java.nio.charset.UnsupportedCharsetException("Unknown charset : " + CHARSET_NAME);
    }
    return customCharset;
}

这篇关于在Webapp中使用自定义字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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