I am brand new to learning how to create java web applications using IntelliJIDEA. I followed the instructions for my first demo project exactly. But my server is giving a 404 Not found error with this message: The requested resource [/DEMO220912B_war_exploded/CalculatorServlet] is not available. I have followed the instructions three times and keep getting the same error. I am using Tomcat 8.5.70, which I already had in the XAMPP folder and have set this in the Run/Debug Configurations. The URL in there is http://localhost:8080/DEMO220912b_war_exploded/. I am using intelliJIDEA 2022.2.1 (Ultimate Edition) free trial version built on August 16, 2022. This is a very new version and I wonder if there's a bug in it. Is there any way I can test this outside of intelliJIDEA to rule out problems with that software? Or can anyone see something I am doing wrong? I'm tearing my hair out. The code is so simple.
Here is the Servlet code:
package com.example.demo220912b;
import java.io.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
@WebServlet(name = "helloServlet", value = "/hello-servlet")
public class HelloServlet extends HttpServlet {
private String message;
public void init() {
message = "Hello World!";
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
// Hello
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>" + message + "</h1>");
out.println("</body></html>");
}
public void destroy() {
}
}
The CalculatorServlet
package com.example.demo220912b;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import java.io.IOException;
@WebServlet(name = "CalculatorServlet", value = "/CalculatorServlet")
public class CalculatorServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int a = Integer.parseInt(request.getParameter("a"));
int b = Integer.parseInt(request.getParameter("b"));
int sum = (a + b);
response.getWriter().println("sum of " + a + " and " + b +" = " + sum);
}
}
The JSP file
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<body>
<h1><%= "Hello World!" %>
</h1>
<form action="CalculatorServlet" method="post">
<p>
Input number A: <input type="number" name="a" required />
</p>
<p>
Input number B: <input type="number" name="b" required />
</p>
<p>
<input type="submit" value="RUN" />
</p>
</form>
</body>
</html>