ヘッダーの名前の取得(getHeaderNames)

広告

リクエストに含まれるヘッダーには名前と値がペアで含まれて居ますがここではヘッダーの名前の取得方法を確認します。

ヘッダの名前を取得するには「HttpServletRequest」インターフェースで定義されている「getHeaderNames」メソッドを使います。

Returns an enumeration of all the header names this request contains.
If the request has no headers, this method returns an empty enumeration. 

Some servlet containers do not allow servlets to access headers using
this method, in which case this method returns null 

Returns:
  an enumeration of all the header names sent with this request; if the
    request has no headers, an empty enumeration; if the servlet container
    does not allow servlets to use this method, null

このメソッドでは、リクエストに含まれる全てのヘッダー名が取得できます。

サンプルプログラム

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

RequestSample7.java

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

public class RequestSample7 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>");

    Enumeration headernames = request.getHeaderNames();
    while (headernames.hasMoreElements()){
      String name = (String)headernames.nextElement();
      sb.append(name);
      sb.append("<br>");
    }

    sb.append("</p>");

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

    out.println(new String(sb));

    out.close();
  }
}

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

D:\ -- servlet-sample
        |
        +-- WEB-INF
             |
             +-- (web.xml)
             |
             +-- classes
                  |
                  +-- (RequestSample7.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>RequestSample7</servlet-name>
    <servlet-class>RequestSample7</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>RequestSample7</servlet-name>
    <url-pattern>/RequestSample7</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/RequestSample7」へブラウザでアクセスして下さい。

getHeaderNames

IE7.0でリクエストを出した場合は上記のように表示されました。同じようにFireFoxでリクエストを出した場合は次のように表示されます。

getHeaderNames

同じリクエストであってもブラウザによってリクエストに含まれるヘッダーの種類は異なります。

( Written by Tatsuo Ikura )

Profile
profile_img

著者 / TATSUO IKURA

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