首页 文章

Tomcat 7采用UTF-8编码

提问于
浏览
0

案例1:我有一个带Ubuntu和Tomcat 7.x的虚拟机 . 如果我将带有特殊字符的数据发送到服务器,我没问题 . 我有一个在任何servlet之前运行的过滤器,并将编码设置为UTF-8 request.setCharacterEncoding("utf-8");

案例2:在同一个虚拟机中,我有其他Tomcat 7.x安装(回复) . 现在我在Tomcat 7.x上安装了JOSSO 1.8.6 . 在此之后,当我尝试使用特殊字符提交POST数据时,servlet无法正确解码 request.getParameter("reportText"); .

我相信JOSSO有任何在我的编码过滤器之前运行的过滤器,并使用了什么使我的过滤器 request.setCharacterEncoding("utf-8") 无用 .

我试图解决这个问题:

  • 我在Tomcat Connector标记(server.xml)上设置了URIEncoding = "utf-8" .

  • 我使用了setCharacterEncoding() .

  • 我在tomcat setenv.sh上设置了 JAVA_OPTS="$JAVA_OPTS -Djavax.servlet.request.encoding=UTF-8 -Dfile.encoding=UTF-8"

  • 我检查了HTTP标头,它们是正确的(Content-Type = text / plain; charset = utf-8)

  • 我启用了Tomcat过滤器 setCharacterEncodingFilter

POST请求由客户端JSP生成

<form id="xx" name="xx" onsubmit="return validateAndSubmitForm(this);" action="submitReport.do" method="post" enctype="application/x-www-form-urlencoded; charset: utf-8;"> xxx </form>

我的JSP标签在上面:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

POST Headers :image with the headers

有任何想法吗?谢谢 .

2 回答

  • 1

    我添加了 server.xml 文件的 Connector 元素的 URIEncoding 属性,该文件是tomcat的配置文件 .

    我的代码如下 .

    <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8082" protocol="HTTP/1.1" redirectPort="8443"/>    
    <Connector URIEncoding="UTF-8" port="8009" protocol="AJP/1.3" redirectPort="8443"/>
    
  • 0

    适合我的解决方案是将URIEncoding添加到ubuntu配置的server.xml文件中 .

    <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8082" protocol="HTTP/1.1" redirectPort="8443"/>    
    <Connector URIEncoding="UTF-8" port="8009" protocol="AJP/1.3" redirectPort="8443"/>
    

相关问题