LoginSignup
10
13

More than 5 years have passed since last update.

【Tomcat】HttpServletResponse#sendRedirectでlocationヘッダが相対パスになる

Last updated at Posted at 2016-01-20

Commons HttpClientで、リダイレクトできなかったはなし。

前提

Commons HttpClientは下のような感じでhttpリクエストを送信することができます。


HttpClient client = new HttpClient();
HttpMethod method = new GetMethod("http://www.apache.org/");
try {
    int statusCode = client.executeMethod(method);
} catch(IllegalArgumentException e) {
    (省略
}

ここでHttpMethodに食わせるURLですが、絶対パスでなければなりません。

今まで Tomcat 8.0.9 でリダイレクトした際のLocationヘッダをそのままHttpMethodにセットして問題なく動いていたのですが、 Tomcat を 8.0.30 にバージョンアップしたとたん

java.lang.IllegalArgumentException: host parameter is null

が出るようになってしまいました。

原因

バージョンアップ前の環境と動作を比較したところ、Locationヘッダがそれぞれ

・バージョンアップ前
Location: http://sample.jp/mywebap/main

・バージョンアップ後
Location: /mywebap/main

となっていました。なんで相対パスになってるん。。。?

うんうん唸りながらTomcatのリリースノート見たら解決。

As per RFC7231 (HTTP/1.1), allow HTTP/1.1 and later redirects to use relative URIs. This is controlled by a new attribute useRelativeRedirects on the Context and defaults to true. (markt)

Tomcat 8.0.30 では、HttpServletResponse#sendRedirect でリダイレクトした時に、Locationヘッダに相対パスをセットするのがデフォルトになったようです。。。

上に書いてあるとおり、Context ディスクリプタに属性を加えると、これまで通りの挙動をしてくれます。

context.xml
(省略)
<Context useRelativeRedirects="false">
(省略)
</Context>

これで無事動くようになりました。

10
13
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
10
13