Tomcat 7.0.35为静态HTML文件设置HTTP响应头内容类型字符集
我使用HTTP连接器从独立的Tomcat7.0.35服务器在单个war文件中提供一些静态HTML文件和servlet 我想通过设置HTTP响应头Tomcat 7.0.35为静态HTML文件设置HTTP响应头内容类型字符集,tomcat,war,content-type,servlet-filters,static-html,Tomcat,War,Content Type,Servlet Filters,Static Html,我使用HTTP连接器从独立的Tomcat7.0.35服务器在单个war文件中提供一些静态HTML文件和servlet 我想通过设置HTTP响应头Content-Type=text/HTML来指定所有静态HTML文件的字符集;字符集=UTF-8 默认情况下,Tomcat为HTML文件提供内容类型=text/HTML(无字符集部分) 我按照以下指示: 但是标题仍然包含Content-Type=text/html,没有;字符集=UTF-8 我的web.xml复制如下。请注意,我尝试将url模式更改为
Content-Type=text/HTML来指定所有静态HTML文件的字符集;字符集=UTF-8
默认情况下,Tomcat为HTML文件提供内容类型=text/HTML
(无字符集部分)
我按照以下指示:
但是标题仍然包含Content-Type=text/html
,没有;字符集=UTF-8
我的web.xml复制如下。请注意,我尝试将url模式
更改为/*
、*
、/index.html
、和index.html
,但这些都不起作用
仅供参考,/index.html文件由Tomcat正确提供(除了缺少的;charset=UTF-8
)。/getdataservlet也正常工作,我已经成功地设置了servlet的响应contenttype=text/html;charset=UTF-8
通过使用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>
字符编码
org.apache.catalina.filters.SetCharacterEncodingFilter
编码
UTF-8
字符编码
/index.html
数据服务器
com.rcg.data.web.DataServlet
数据服务器
/获取数据
30
您是否尝试将MIME映射设置为包含字符集
<mime-mapping>
<extension>html</extension>
<mime-type>text/html;charset=UTF-8</mime-type>
</mime-mapping>
html
text/html;字符集=UTF-8
检查以下答案[如何让UTF-8在Java webapps中工作?][1][1]: