hinataka
@hinataka

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

サーブレット使用で404エラー

解決したいこと

VS Code、Java初心者です
サーブレットを使ってWebページを遷移させたいが、404エラーになります。
(http://localhost:8080/demo/で「Java Servletを呼び出す」というリンクを踏むと、サーブレットでメッセージを表示する)
解決方法をぜひ教えてください。
情報足りないところありましたら、教えてください。

発生している問題・エラー

 オリジンサーバーは、ターゲットリソースの現在の表現を見つけられなかったか、またはそれが存在することを開示するつもりはありません。

ディレクトリのスクショ
スクリーンショット (16).png

該当するソースコード

HelloServlet.servlet
package com.example;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // レスポンスのコンテンツタイプを設定
        response.setContentType("text/html;charset=UTF-8");

        // レスポンスにメッセージを送信
        response.getWriter().println("<h2>こんにちは!Java Servlet からのメッセージです。</h2>");
    }
}
index.html
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java Servlet Example</title>
</head>
<body>
    <h1>Java Servlet 呼び出し</h1>
    <p>以下のボタンを押すと、Java Servlet が呼び出されます。</p>
    <a href="hello">Java Servletを呼び出す</a>
</body>
</html>

pom.xml
<?xml version="1.0" encoding="UTF-8"?>

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>demo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>demo Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>demo</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

自分で試したこと

・tomcat、vs codeの再起動、再デプロイ
・Tomcatのログ(logs/catalina.out)にエラーが記録されていないことを確認
・ネット記事やQiitaを読んだが解決に至らず
(追記)htmlのa href="/hello"にカーソルを合わせると、「リンク先を表示」と出て、クリックするとファイルが見つからない旨のエラーが出る
→相対パスを指定で解決

0

3Answer

404 応答が返ってくるということは、名前解決ができてクライアントからの要求はサーバーに届き、サーバーで url に指定されているリソースを探したが見つからなかったということです。原因はほとんどが指定した url が間違っているということです。なのでそこを調べてください。

間違いには、単純なスペルミス、相対パスの使い方の間違い、サーバーでのルーティングの間違い、要求先サーバーを間違えたなどがあります。ほとんどは質問者さんでないと分からないことです。

1Like

Comments

  1. @hinataka

    Questioner

    ご助言ありがとうございます
    何のミスか今一度探そうと思います

クローズされているようですが、HelloServlet.servletの拡張子は.javaだったりしませんか?

1Like

Comments

  1. @hinataka

    Questioner

    ご返信ありがとうございます。
    すみません、それは「.javaが正しい」という意味か、
    「.servletのはずが.javaになってしまっているのでは」という意味、
    どちらでしょうか

  2. すみません「.javaが正しい」という意味です。

  3. @hinataka

    Questioner

    普通そっちですよね、すみません
    今プロジェクトいちから直してるので、試してみます

Comments

  1. @hinataka

    Questioner

    なんとか表示できたので、以下に覚えてる限りでやったこと書いておきます

  2. @hinataka

    Questioner

    やったこと(<>全角に変換してます)
    ・a href=http://localhost:8080/demo/helloに変更
    ・pom.xmlに
    <dependency>
    <groupId>jakarta.servlet</groupId>
    <artifactId>jakarta.servlet-api</artifactId>
    <version>5.0.0</version> <!-- Tomcat 10.x用 -->
    <scope>provided</scope>
    </dependency>
    を追記、
    import jakarta.servlet.ServletException;
    import jakarta.servlet.annotation.WebServlet;
    import jakarta.servlet.http.HttpServlet;
    import jakarta.servlet.http.HttpServletRequest;
    import jakarta.servlet.http.HttpServletResponse;
    に変更
    (Tomcatのバージョン
    Tomcat 10.x → jakarta.servlet API を使用します。
    Tomcat 9.x 以前 → javax.servlet API を使用します。)
    ・web.xmlの
    <servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/hello</url-pattern>
    </servlet-mapping>
    を削除
    (catalina.2024-11-22.log(tomcatのログ)に「Caused by: java.lang.IllegalArgumentException: サーブレット [HelloServlet] と [com.example.HelloServlet] を同じ url-pattern [/hello] にマッピングすることはできません。」とエラー出ていたので)
    (追記)
    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>23</maven.compiler.source>
    <maven.compiler.target>23</maven.compiler.target>
    </properties>
    Java version: 23だったので、5だか6だかになっていたのを23に変更

    ※解決できた要因
    とにかくエラーを探してchatGPTに見てもらう
    でも自分でもエラーログ(英語か...)を読解してとにかくトライアンドエラー
    ご助言いただいた方にも感謝
    機械音痴なので、vs codeでサーブレット使える環境作るのに2日かかりました。

  3. 原因は何だったのでしょうか? ルーティングの問題だったのですか?

    以下、本題と関係ない話ですが。

    <>全角に変換してます

    ` で前後を囲うと良いです。例えば以下は半角に戻して前後を囲ってます。

    <groupId>jakarta.servlet</groupId>

    url も同様にして、リンクになるのを避けることができます。

    http://localhost:8080/demo/hello

  4. @hinataka

    Questioner

    ルーティングの問題でした。

    <groupId>jakarta.servlet</groupId>
    http://localhost:8080/demo/hello
    できました
    情報ありがとうございます。

Your answer might help someone💌