Unable to change charset from ISO-8859-1 to UTF-8 in glassfish 3.1

前端 未结 4 1328
北恋
北恋 2020-12-01 13:12

I am having problems to change the charset in my web application response from ISO-8859-1 (default) to UTF-8. I already added the VM option -Dfile.encoding=UTF-8

相关标签:
4条回答
  • 2020-12-01 13:59

    The -Dfile.encoding is a Oracle JVM specific setting as to how to read Java source files. This doesn't have any influence on the charset as specified in the Content-Type header of the HTTP response.

    You need to add the following to your web.xml in order to send the response of all JSPs as UTF-8 and to let it set the appropriate charset in the response header.

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

    See also:

    • Unicode - How to get the characters right?
    0 讨论(0)
  • 2020-12-01 14:00

    Try adding:

        <filter> 
                <filter-name>Set Character Encoding</filter-name> 
                <filter-class>filters.SetCharacterEncodingFilter</filter-class> 
                <init-param> 
                        <param-name>encoding</param-name> 
                        <param-value>UTF_8</param-value> 
                </init-param> 
        </filter> 
        <filter-mapping> 
                <filter-name>Set Character Encoding</filter-name> 
                <url-pattern>/*</url-pattern> 
        </filter-mapping>
    

    to your web.xml... According to http://wiki.metawerx.net/wiki/Web.xml these stanza will set your encoding to UTF_8.

    0 讨论(0)
  • 2020-12-01 14:07

    For UTF-8 fonts on Glassfish3 (Log files, etc):

    Go to Server-config > JVM Settings > JVM Options > Add option (-Dfile.encoding=UTF8).

    If you are not on -server mode then go to default-config > JVM Settings > JVM Options

    0 讨论(0)
  • 2020-12-01 14:15

    In order to define a standard response charset other than the default ISO-8859-1 for GlassFish (or Tomcat, or any other Servlet container) you will need to put a filter that calls response.setCharacterEncoding. Here is how:
    1. In your web.xml define the filter:

    <filter>
      <filter-name>Set Response Character Encoding</filter-name>
      <filter-class>com.omrispector.util.SetCharacterEncodingFilter</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>/*</url-pattern>
    </filter-mapping>
    

    2. Here is the filter implementation:

    package com.omrispector.util;
    
    import javax.servlet.*;
    import java.io.IOException;
    import java.nio.charset.Charset;
    import java.util.logging.Logger;
    
    /**
     * Created by Omri at 03/12/13 10:39
     * Sets the character encoding to be used for all sources returned
     * (Unless they override it later)
     * This is free for use - no license whatsoever.
     */
    public class SetCharacterEncodingFilter implements Filter {
    
      private String encoding = null;
      private boolean active = false;
    
      private static final Logger logger =
          Logger.getLogger(SetCharacterEncodingFilter.class.getName());
    
      /**
       * Take this filter out of service.
       */
      @Override
      public void destroy() {
        this.encoding = null;
      }
    
      /**
       * Select and set (if specified) the character encoding to be used to
       * interpret request parameters for this request.
       */
      @Override
      public void doFilter(ServletRequest request, ServletResponse response,
                           FilterChain chain)
          throws IOException, ServletException {
        if (active) response.setCharacterEncoding(encoding);
        chain.doFilter(request, response);
      }
    
      /**
       * Place this filter into service.
       */
      @Override
      public void init(FilterConfig filterConfig) throws ServletException {
        this.encoding = filterConfig.getInitParameter("encoding");
        try {
          Charset testCS = Charset.forName(this.encoding);
          this.active = true;
        } catch (Exception e) {
          this.active = false;
          logger.warning(encoding + " character set not supported ("+e.getMessage()+"). SetCharacterEncodingFilter de-activated.");
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题