The essence of the servlet is this - there is a form on it with two input fields, I enter numbers into them, after which the servlet should work, which will return the sum of these two numbers. File code for the form below (when you click on the button, the Get method of the CalculateServlet servlet should work)
<form action="CalculateServlet" method="GET"> <p align="center"><font size="5"><b>CalculationForm</b></font></p> <TABLE ALIGN="center" height="57"> <TR> <TD > <b>Enter First Number:</b> </TD> <TD > <input type="text" name="number1" size="20" > </TD> </TR> <TR> <TD > <b>Enter Second Number:</b> </TD> <TD > <input type="text" name="number2" size="20" > </TD> </TR> <TR align="center"> <TD colspan=5> <input type="Submit" value="Add"> </TD> </TR> </TABLE> </form> Here is the listing of the servlet itself
@WebServlet(urlPatterns = "/CalculateServlet") public class CalculateServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* Получение чисел, введенных пользователем в HTML-форме */ int num1 = Integer.parseInt(request.getParameter("number1")); int num2 = Integer.parseInt(request.getParameter("number2")); /* Нахождение суммы чисел, введенных пользователем. */ int result = num1 + num2; response.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); PrintWriter pw = response.getWriter(); /* вывод результата */ pw.println("<h3>The result of the calculation is: " +Integer.toString(result)+"<h3>"); pw.close(); } Error 404 - / Calculate_2 (project name) / CalculateServlet (servlet name) crashes when the application starts. The requested resource is not available. I understand that a servlet cannot somehow be found by a button handler, which is on the html form?
http://localhost:8080/Calculate_2/CalculateServlet. Didn't set Context root to a value other than the default? Then the address will behttp://localhost:8080/Ваш_context_root/CalculateServletIn addition, as practice has shown, some use embedded servers, where the contextpath is formed differently. The address may also behttp://localhost:8080/CalculateServlet. (Instead of port 8080, substitute your own) - Sergey