Geronimo 和 Tomcat 中的默认编码

发布于 2024-10-19 04:46:12 字数 797 浏览 4 评论 0原文

我正在寻找一种实用的方法来为带有 Tomcat 的 Geronimo 3 的 HTTP 响应设置默认字符编码。似乎有很多解决方法,但没有任何方法可以像 Apache Httpd 那样轻松地在某些文件中设置某些属性。 (Apache Httpd 将 AddDefaultCharset 用于所有响应。)我尝试将属性 useBodyEncodingForURIURIEncoding 添加到 < var/catalina/server.xml 中的 /code> 元素。

<Connector name="TomcatWebConnector"
    port="${HTTPPort + PortOffset}"
    address="${ServerHostname}"
    protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="${HTTPSPort + PortOffset}"
    useBodyEncodingForURI="true"
    URIEncoding="UTF-8"
    executor="DefaultThreadPool"/>

但这不起作用。我仍然收到标头 Content-Type text/html;charset=ISO-8859-1 作为响应。另一个解决方案是以编程方式更改应用程序中的标头(例如通过过滤器或阀门),但我不认为它是系统性的。

I'm looking for functional way to set default character encoding for HTTP responses for Geronimo 3 with Tomcat. It seems there is a lot of work-arounds, but not any way to easily set some property in some file like in case of Apache Httpd. (Apache Httpd has AddDefaultCharset used for all responses.) I've tried add attributes useBodyEncodingForURI and URIEncoding to <connector> elements in var/catalina/server.xml.

<Connector name="TomcatWebConnector"
    port="${HTTPPort + PortOffset}"
    address="${ServerHostname}"
    protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="${HTTPSPort + PortOffset}"
    useBodyEncodingForURI="true"
    URIEncoding="UTF-8"
    executor="DefaultThreadPool"/>

But it does not work. I still get header Content-Type text/html;charset=ISO-8859-1 in response. Another solution is to programaticaly change headers in application (e.g. via filter or valve), but I don't find it systemic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅浅 2024-10-26 04:46:12

我回到两个月前提出的问题。我现在知道使用过滤器的解决方案不是任何解决方法,而是常见的方法。只需编写一个过滤器类:

package eu.barbucha.tests;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EncodingSettingFilter implements Filter {
    private final String enc = "utf-8";

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse resp, FilterChain fc)
            throws IOException, ServletException {
        request.setCharacterEncoding(enc);
        resp.setCharacterEncoding(enc);
        fc.doFilter(request, resp);
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
    }
}

并为该过滤器分配 WEB-INF/web.xml 文件中的所有 URI:

<filter>
    <description>Filter setting encoding</description>
    <filter-name>enc-filter</filter-name>
    <filter-class>eu.barbucha.tests.EncodingSettingFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>enc-filter</filter-name>
    <url-pattern>*</url-pattern>
</filter-mapping>

仅此而已。

I returned to my question I had asked two months ago. I know now that the solution using a filter is not any workaround, but common way. Just write a filter class:

package eu.barbucha.tests;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EncodingSettingFilter implements Filter {
    private final String enc = "utf-8";

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse resp, FilterChain fc)
            throws IOException, ServletException {
        request.setCharacterEncoding(enc);
        resp.setCharacterEncoding(enc);
        fc.doFilter(request, resp);
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
    }
}

And assign the filter with all URIs in the WEB-INF/web.xml file:

<filter>
    <description>Filter setting encoding</description>
    <filter-name>enc-filter</filter-name>
    <filter-class>eu.barbucha.tests.EncodingSettingFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>enc-filter</filter-name>
    <url-pattern>*</url-pattern>
</filter-mapping>

That's all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文