Skip to main content
1 / 2
Pota
  • 3
  • 4

JSPでフォームを送信した後、Servletが見つからずHTTP 404エラーになってしまいます。

index.jspで計算ボタンを押したらServlet.javaへ値を送り、Servlet.javaからservletResult.jspへ値を送って表示させようとしているのですが、Eclipseで実行し計算ボタンを押すとServlet.javaが見つからないというエラーが出てしまいます。
context.xmlは何もいじってないのですが、xmlが原因でエラーになることがあったりするのでしょうか?よろしくお願いします。

バージョン
Eclipse IDE 2024-06 (4.32.0)
java 22.0.2 2024-07-16
Tomcat10 (Java21)

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="../header.html" %>

<p>...</p>
<select id="learningOptions" name="learningOptions">
    <option value="csharp">C#</option>
    <option value="java">Java</option>
    <option value="python">Python</option>
</select>

<p>...</p>
<select id="actionOptions" name="actionOptions" onchange="updateSubOptions()">
     <option value="calc_text" <%= "calc_text".equals(request.getParameter("actionOptions")) ? "selected" : "" %>>テキストを表示/計算</option>
     <option value="app" <%= "app".equals(request.getParameter("actionOptions")) ? "selected" : "" %>>アプリ</option>
</select>
 <form action="/Servlet" method="post">
    <div id="calcTextInput" style="display:<%= "calc_text".equals(request.getParameter("actionOptions")) ? "block" : "none" %>;">
        <label for="inputBox">テキスト入力ボックス:</label>
        <input type="text" id="inputBox" name="inputBox">
        <br>
        <label for="calculation">計算(半角数字と+,-,*,/のみ使用可能)</label>
        <input type="text" id="calculation" name="calculation" onkeypress="return isAllowedCharacter(event)">
    </div>
    <input type="submit" value="計算">
</form>

 <div id="appOptions" style="display:<%= "app".equals(request.getParameter("actionOptions")) ? "block" : "none" %>;">
    <label for="appSubOptions">詳細を選択してください:</label>
    <select id="appSubOptions" name="appSubOptions">
        <option value="quiz">クイズ</option>
        <option value="fastestFinger">早押し</option>
    </select>
</div>

<script>
    function updateSubOptions(){
        var actionDropdown = document.getElementById("actionOptions");
        var selectedValue = actionDropdown.value;
        
        var calcTextInput = document.getElementById("calcTextInput");
        var appOptions = document.getElementById("appOptions");
        
        if (selectedValue === "calc_text"){
            calcTextInput.style.display = "block";
            appOptions.style.display = "none";
        } else if (selectedValue === "app") {
            calcTextInput.style.display = "none";
            appOptions.style.display = "block";
        } else {
            calcTextInput.style.display = "none";
            appOptions.style.display = "none";
        }
    }
    
    function isAllowedCharacter(event) {
        var char = String.fromCharCode(event.which);
        if (!/[0-9+\-*/%]/.test(char)){
            event.preventDefault();
        }
    }
    
    window.onload = function(){
        updateSubOptions();
    };
</script>

<%@include file="../footer.html" %>

Servlet.java

package servlet;

import java.io.IOException;
import java.io.PrintWriter;

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

import lexer.LexerJava;

@WebServlet(urlPatterns={"/Servlet"})
public class Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(
            HttpServletRequest request, HttpServletResponse response
    ) throws ServletException, IOException {
        request.getRequestDispatcher("/result.jsp").forward(request, response);
    }
    
    protected void doPost(
        HttpServletRequest request, HttpServletResponse response
    ) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        
        String inputBox = request.getParameter("inputBox");
        String calculation = request.getParameter("calculation");
        
        LexerJava lexer = new LexerJava();
        String tokenizedResult = "";
        try {
            tokenizedResult = lexer.init(calculation).tokenizeToString();
            } catch (Exception e) {
                e.printStackTrace();
            }
        
        out.println(inputBox);
        out.println(calculation);
        out.println(tokenizedResult);
        out.println("a");
        
        request.setAttribute("inputBox", inputBox);
        request.setAttribute("calculation", calculation);
        request.setAttribute("tokenizedResult", tokenizedResult);
        
        request.getRequestDispatcher("/servletResult.jsp").forward(request, response);
    }
}

servletResult.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="../header.html" %>
    <%
        request.setCharacterEncoding("UTF-8");
    %>
    <p>text:<%= request.getAttribute("inputBox") %></p>
    <p>計算結果:<%= request.getAttribute("calculation") %></p>
    <p><%= request.getAttribute("tokenizedResult") %></p>
<%@include file="../footer.html" %>

ファイルの階層

Pota
  • 3
  • 4