【发布时间】:2013-01-20 22:21:26
【问题描述】:
我正在使用 HTTP 连接器从独立的 Tomcat 7.0.35 服务器在单个 war 文件中提供一些静态 HTML 文件和一个 servlet。
我想通过设置HTTP响应头Content-Type=text/html;charset=UTF-8来指定所有静态HTML文件的字符集。
Tomcat 默认使用Content-Type=text/html(无字符集部分)提供 HTML 文件。
我按照以下说明操作:
http://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q8
但标题仍然包含Content-Type=text/html,但没有;charset=UTF-8
我的 web.xml 转载如下。请注意,我尝试将url-pattern 更改为/*、*、/index.html 和index.html,但这些都不起作用。
仅供参考,Tomcat 正在正确地提供 /index.html 文件(除了缺少的 ;charset=UTF-8)。 /getData servlet 也正常工作,我已经使用response.setContentType("application/json;charset=UTF-8"); 成功设置了servlet 的响应Content-Type=text/html;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>
【问题讨论】:
-
查看以下答案 [How to get UTF-8 working in Java webapps?][1] [1]: stackoverflow.com/questions/138948/…
标签: tomcat war content-type servlet-filters static-html