リダイレクト(redirect)

広告

リダイレクトは他のサーブレットやHTMLファイルに処理を移します。フォワードと似ていますが、フォワードはサーバ内で次のサーブレットなどへ処理を移しますが、リダイレクトの場合はクライアントに対してリダイレクト先のサーブレットなどを見に行くように指示を出すだけです。その為、クライアント側でも処理が別のページ移ったことを認識します。

またフォワードの場合はクライアントから送られ来たパラメータなどを含むリクエストをそのままフォワード先に移しますが、リダイレクトの場合は次の見るべきサーブレットやHTMLファイルなどをクライアントに指示するだけですのでクライアントから送られてきたリクエストの中身などはリダイレクト先には渡されません。

例えば何かの処理をしてエラーだった場合にはエラーページへ飛ばすとか、データベースの処理だけをするサーブレットを呼び出した後に処理が終わったらサーブレットでは何も出力を行わずに特定のページへ飛ばすなどに使います。リダイレクトの場合には、別のサーバにあるURLへ飛ばすことも可能です(フォワードの場合は同一サーバ内しか行えません)。

リダイレクトするには「HttpServletResponse」インターフェースで定義されている"sendRedirect"メソッドを使います。

Sends a temporary redirect response to the client using the specified 
redirect location URL. This method can accept relative URLs; the servlet 
container must convert the relative URL to an absolute URL before sending 
the response to the client. If the location is relative without a leading 
'/' the container interprets it as relative to the current request URI. 
If the location is relative with a leading '/' the container interprets 
it as relative to the servlet container root. 

If the response has already been committed, this method throws an 
IllegalStateException. After using this method, the response should be 
considered to be committed and should not be written to. 

Parameters:
  location - the redirect location URL 
Throws: 
  java.io.IOException - If an input or output exception occurs 
  java.lang.IllegalStateException - If the response was committed or if 
    a partial URL is given and cannot be converted into a valid URL

"sendRedirect"メソッドの引数にはリダイレクトしたいURLを指定します。

利用方法としては下記のようになります。

public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws IOException, ServletException{

  String url = "/dispatch/redirecttest";

  response.sendRedirect(url);
}

サンプルプログラム

では簡単に試してみます。

web.xmlファイルは下記のようにしました。

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  version="2.4">

  <servlet>
    <servlet-name>redirecttest</servlet-name>
    <servlet-class>RedirectTest3</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>redirecttest</servlet-name>
    <url-pattern>/redirecttest</url-pattern>
  </servlet-mapping>
</web-app>

プログラムは下記の通りです。今回は呼び出したサーブレットでログだけを記録して、別のHTMLファイルへリダイレクトしています。

RedirectTest3.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class RedirectTest3 extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException{

    response.setContentType("text/html; charset=Shift_JIS");
    PrintWriter out = response.getWriter();

    log("アクセスされました");

    String url = "/dispatch/redirect.html";
    response.sendRedirect(url);
  }
}

次はリダイレクト先のHTMLファイルです。Webアプリケーションのルートに配置してあります。

redirect.html

<html>
<head>
<title>リダイレクトテスト</title>
</head>
<body>

<p>こんにちは</p>

</body>
</html>

上記をコンパイル後に「d:\servlet-sample\dispatch\WEB-INF\classes\」ディレクトリにクラスファイルを移動した後で、ブラウザで「http://localhost:8080/dispatch/redirecttest」へアクセスしてみます。

リダイレクト

直接呼び出したサーブレットはログの記録だけしてすぐにリダイレクトされますので、リダイレクト先のHTMLファイルをクライアントが再度読み出して自動的に表示されます。

またブラウザのURLの箇所も、リダイレクト先の「redirect.html」にアクセスされたように表示されています。

リダイレクト

フォワードの場合は呼び出したサーブレットが、サーバ内部で別のサーブレットなどに処理をそのまま移す為、クライアント側では処理が移ったことを把握できません。リダイレクトの場合にはサーバがクライアントに別のURLを見に行くように指示するだけなので、クライアントはリダイレクト先のURLを自分で見に行ったかのような挙動をするわけです。

( Written by Tatsuo Ikura )

Profile
profile_img

著者 / TATSUO IKURA

プログラミングや開発環境構築の解説サイトを運営しています。