サーブレット/JSPのビルド(Maven)とデプロイ(Tomcat)うまく行かない
解決したいこと
JavaのプログラムをMavenでビルドして、Tomcatで動かしたいんですけど、うまく行きません(404 Not foundになる)
発生している問題・エラー
ファイル構成
Tomcat 実行構成
該当するソースコード
動かしたいプログラムは以下のソースコードです。
HelloServlet.java
package servletTry;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import jakarta.servlet.ServletException;
import jakarta.sevlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/servletSample")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String[] luckArray = {"awesome", "good", "bad"};
int index = (int)(Math.random() * luckArray.length);
String luck = luckArray[index];
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = sdf.format(date);
response.setContentType("text/html; charset = UTF-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset=\"utf-8\">");
out.println("<title>Servlet HelloServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>" + dateStr + "の運勢は" + luck + "</p>");
out.println("</body>");
out.println("</html>");
}
}
自分で試したこと
user@buridaikon:/mnt/c/Users/count/IdeaProjects/servletJava$ mvn clean
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< org.example:servletJava >-----------------------
[INFO] Building servletJava Maven Webapp 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ servletJava ---
[INFO] Deleting /mnt/c/Users/count/IdeaProjects/servletJava/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.624 s
[INFO] Finished at: 2025-01-19T18:24:28+09:00
[INFO] ------------------------------------------------------------------------
user@buridaikon:/mnt/c/Users/count/IdeaProjects/servletJava$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< org.example:servletJava >-----------------------
[INFO] Building servletJava Maven Webapp 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ servletJava ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ servletJava ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ servletJava ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /mnt/c/Users/count/IdeaProjects/servletJava/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ servletJava ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ servletJava ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-war-plugin:2.2:war (default-war) @ servletJava ---
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/home/user/.m2/repository/com/thoughtworks/xstream/xstream/1.3.1/xstream-1.3.1.jar) to field java.util.Properties.defaults
WARNING: Please consider reporting this to the maintainers of com.thoughtworks.xstream.core.util.Fields
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[INFO] Packaging webapp
[INFO] Assembling webapp [servletJava] in [/mnt/c/Users/count/IdeaProjects/servletJava/target/servletJava]
[INFO] Processing war project
[INFO] Copying webapp resources [/mnt/c/Users/count/IdeaProjects/servletJava/src/main/webapp]
[INFO] Webapp assembled in [371 msecs]
[INFO] Building war: /mnt/c/Users/count/IdeaProjects/servletJava/target/servletJava.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.700 s
[INFO] Finished at: 2025-01-19T18:26:24+09:00
[INFO] ------------------------------------------------------------------------
0