LoginSignup
5
8

More than 3 years have passed since last update.

Tomcat8 から useBodyEncodingForURI の内容が変わっている?

Last updated at Posted at 2018-09-02

Tomcat7からTomcat8に移行した時のメモ

Tomcat8 から useBodyEncodingForURI の内容が変わっている?

Tomcat7 の時にGETのクエリパラメータに日本語を含んでいた場合、文字化けしていたのですが、
server.xml を useBodyEncodingForURI="true" とするのには影響範囲が大きすぎたので、
以前は下記のように、問題のあるアプリを個別に修正していました。

String param = new String(parameter.getBytes("ISO_8859_1"));

しかし、Tomcat8 からは、上記対応をしていると文字化けしていまい、むしろ対応が不要になっています。

Tomcat7: http://tomcat.apache.org/tomcat-7.0-doc/config/ajp.html
Tomcat8: http://tomcat.apache.org/tomcat-8.0-doc/config/ajp.html

ドキュメントを見比べてみると、URIEncodingの説明が違う。

Tomcat7

This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, ISO-8859-1 will be used.

GET時のリクエストパラメタのURIエンコードを指定します。 URIEncodingが指定されていない場合は、 ISO-8859-1 が使用されます。

Tomcat8

This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, UTF-8 will be used unless the org.apache.catalina.STRICT_SERVLET_COMPLIANCE system property is set to true in which case ISO-8859-1 will be used.

GET時のリクエストパラメタのURIエンコードを指定します。 URIEncodingが指定されていない場合は、 UTF-8 が使用されます。
ただし、システムプロパティ org.apache.catalina.STRICT_SERVLET_COMPLIANCE を trueにすると ISO-8859-1 が使用されます。

URIEncodingを指定していない時の動作が変わっている。

対応

試しにシステムプロパティorg.apache.catalina.STRICT_SERVLET_COMPLIANCEtrueにしてみようと思いましたが、
org.apache.catalina.STRICT_SERVLET_COMPLIANCEは、Tomcatのバージョン変化による差を解消するためのオプションみたいで、
影響範囲が広すぎるので、URIEncodingに Tomcat7のデフォルトである、ISO-8859-1を指定することにしました。

  • URIEncoding="ISO-8859-1"を追加
conf/server.xml
<Connector port="8080" protocol="HTTP/1.1"
           redirectPort="8443"
           URIEncoding="ISO-8859-1" />
5
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
8