Web系開発メモ

Java, C#, HTML, CSS, JavaScript のことなどを書いてます。

Servlet サーブレットでHTTPリクエストを受信する方法

Javaサーブレットで、HTTPリクエストを受け取る方法を書いていきます。

バージョン

ブログ執筆時の製品バージョンは以下の通りです。

目次

  1. ディレクトリ階層の作成
  2. サーブレットの作成
  3. pom.xml の作成
  4. 動作確認(GETメソッド)
  5. 動作確認(GETメソッド以外)

1. ディレクトリ階層の作成

以下のコマンドで、サンプルプロジェクト sample-servletディレクトリ階層を作成します。

mkdir sample-servlet
cd sample-servlet
mkdir src\main\java
mkdir src\main\webapp\WEB-INF

サーブレットのコードは、src/main/java 配下に置きます。

2. サーブレットの作成

HTTPメソッド(GET, POST, PUT, DELETE)を受信して、文字列を標準出力するサーブレットを作成します。

src/main/java/org/example/request/ReceiveServlet.java

package org.example.request;

import java.io.IOException;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet("/req/receive")
@SuppressWarnings("serial")
public class ReceiveServlet extends HttpServlet {
  public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    System.out.println("Process Http Get.");
  }
  public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    System.out.println("Process Http Post.");
  }
  public void doPut(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    System.out.println("Process Http Put.");
  }
  public void doDelete(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    System.out.println("Process Http Delete.");
  }
}

@WebServlet を付けて、/req/receive のリクエストを処理できるようにしています。

3. pom.xml の作成

フォルダ sample-servletpom.xml を作成します。

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>sample-servlet</artifactId>
  <version>1.0.0</version>
  <packaging>war</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>jakarta.servlet</groupId>
      <artifactId>jakarta.servlet-api</artifactId>
      <version>5.0.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.4.0</version>
      </plugin>
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>11.0.15</version>
        <configuration>
          <scan>1</scan>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Mavenサーブレットコンテナを起動できるように、build で Jetty Plugin を定義しています。

4. 動作確認(GETメソッド)

以下のコマンドでコンテナ(Jetty Maven Plugin)を起動します。

sample-servlet> mvn jetty:run

起動後にブラウザで以下の URL を開きます。

http://localhost:8080/req/receive

ブラウザで開くと、以下の文字列が標準出力されます。

Process Http Get.

5. 動作確認(GETメソッド以外)

GETメソッド以外は、curl でメソッドを指定して確認することができます。

curl -X POST http://localhost:8080/req/receive