本文介绍了Tomcat 7.0.35为静态HTML文件设置HTTP响应标头Content-Type字符集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HTTP连接器从独立的Tomcat 7.0.35服务器在单个war文件中提供一些静态HTML文件和一个servlet.

I am serving some static HTML files and a servlet all in a single war file from a standalone Tomcat 7.0.35 server using the HTTP Connector.

我想通过设置HTTP响应标头Content-Type=text/html;charset=UTF-8来指定所有静态HTML文件的字符集.

I want to specify the charset of all the static HTML files by setting the HTTP response header Content-Type=text/html;charset=UTF-8.

Tomcat默认情况下使用Content-Type=text/html提供HTML文件(无字符集部分).

Tomcat by default serves HTML files with Content-Type=text/html (no charset portion).

我按照以下指示进行操作:

I followed the instructions at:

http://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q8

但是标题仍然包含Content-Type=text/html而没有;charset=UTF-8

But the header still contains Content-Type=text/html without the ;charset=UTF-8

我的web.xml复制如下.请注意,我尝试将url-pattern更改为/**/index.htmlindex.html,但是这些都不起作用.

My web.xml is reproduced below. Note that I tried changing the url-pattern to /*, *, /index.html, and index.html, but none of these worked.

仅供参考,Tomcat已正确地为/index.html文件提供服务(缺少的;charset=UTF-8除外)./getData servlet也可以正常工作,并且我已经通过使用response.setContentType("application/json;charset=UTF-8");成功设置了servlet的响应Content-Type=text/html;charset=UTF-8.

FYI, the /index.html file is being correctly served by Tomcat (except for the missing ;charset=UTF-8). The /getData servlet is also working correctly, and I have successfully set the servlet's responses Content-Type=text/html;charset=UTF-8 by using response.setContentType("application/json;charset=UTF-8");.

感谢您的帮助.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <filter>
        <filter-name>CharacterEncoding</filter-name>
        <filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncoding</filter-name>
        <url-pattern>/index.html</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>DataServlet</servlet-name>
        <servlet-class>com.rcg.data.web.DataServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>DataServlet</servlet-name>
        <url-pattern>/getData</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
</web-app>

推荐答案

您是否尝试过设置MIME映射以包括字符集?

Have you tried setting the MIME mapping to include the charset?

<mime-mapping>
    <extension>html</extension>
    <mime-type>text/html;charset=UTF-8</mime-type>
</mime-mapping>

这篇关于Tomcat 7.0.35为静态HTML文件设置HTTP响应标头Content-Type字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 02:11