リクエスト方法の取得(getScheme)

広告

サーブレットに対するリクエスト方法としてはHTTPの他にもHTTPSやFTPなどがあり、またHTTPであってもプロトコルにもHTTP1.0とHTTP1.1があります。ここではリクエストがどのような方法で送られたのかをサーブレット側で確認する方法を確認します。

まずはリクエストのスキームを取得します。スキームを取得するには「HttpServletRequest」インターフェースの親インターフェースである「ServletRequest」インターフェースで定義されている「getScheme」メソッドを使います。

Returns the name of the scheme used to make this request, for example,
http, https, or ftp. Different schemes have different rules for 
constructing URLs, as noted in RFC 1738. 

Returns:
  a String containing the name of the scheme used to make this request

このメソッドでは、リクエストのスキームが取得できます。例えばHTTP又はHTTPSなどが取得できますのでSSL通信が行われているかどうかの判別に利用できます。

次にプロトコルとプロトコルのバージョンの確認方法です。プロトコルバージョンを取得するには「HttpServletRequest」インターフェースの親インターフェースである「ServletRequest」インターフェースで定義されている「getProtocol」メソッドを使います。

Returns the name and version of the protocol the request uses in the form 
protocol/majorVersion.minorVersion, for example, HTTP/1.1. For HTTP servlets, 
the value returned is the same as the value of the CGI variable 
SERVER_PROTOCOL. 

Returns:
  a String containing the protocol name and version number

例えばHTTPであれば「HTTP/1.0」や「HTTP/1.1」などのようにプロトコルとプロトコルバージョンが取得できます。「HTTP/1.1」の場合だけ行いたい処理がある時などに利用できます。

最後にHTTP通信だった場合の使用メソッド名を取得する方法です。HTTPメソッド名を取得するには「HttpServletRequest」インターフェースで定義されている「getMethod」メソッドを使います。

Returns the name of the HTTP method with which this request was made, for 
example, GET, POST, or PUT. Same as the value of the CGI variable 
REQUEST_METHOD. 

Returns:
  a String specifying the name of the method with which this request
    was made

このメソッドによって「GET」や「POST」などのHTTPメソッド名が取得できます。

サンプルプログラム

では簡単なサンプルで試して見ます。

今回はサーブレットを直接呼び出してリクエストから各種情報を取り出してみます。

RequestSample6.java

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

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

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

    StringBuffer sb = new StringBuffer();

    sb.append("<html>");
    sb.append("<head>");
    sb.append("<title>テスト</title>");
    sb.append("</head>");
    sb.append("<body>");

    sb.append("<p>");
    sb.append("getScheme:");
    sb.append(request.getScheme());
    sb.append("</p>");

    sb.append("<p>");
    sb.append("getProtocol:");
    sb.append(request.getProtocol());
    sb.append("</p>");

    sb.append("<p>");
    sb.append("getMethod:");
    sb.append(request.getMethod());
    sb.append("</p>");

    sb.append("</body>");
    sb.append("</html>");

    out.println(new String(sb));

    out.close();
  }
}

サンプルプログラムをコンパイルして作成した「RequestSample6.class」ファイルを別途作成した「web.xml」ファイルを次のように配置します。

D:\ -- servlet-sample
        |
        +-- WEB-INF
             |
             +-- (web.xml)
             |
             +-- classes
                  |
                  +-- (RequestSample6.class)

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>RequestSample6</servlet-name>
    <servlet-class>RequestSample6</servlet-class>
  </servlet>

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

コンテキストファイルを作成し「(Tomcatをインストールしたディレクトリ)\Tomcat 5.5\conf\Catalina\localhost\」ディレクトリに「sample.xml」ファイルとして保存します。内容は以下の通りです。

<Context path="/sample"
docBase="d:/servlet-sample/sample">
</Context>

準備は以上です。ではTomcatを再起動してから「http://localhost:8080/sample/RequestSample6」へブラウザでアクセスして下さい。

getScheme

( Written by Tatsuo Ikura )

Profile
profile_img

著者 / TATSUO IKURA

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