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?

  • Your servlet is responsible at CalculateServlet, I do not think that you should specify the name of the project. Since the Get method is used, you can test it right in the address bar of the browser - Mikhail Chibel
  • what address do you use to load the page? The page address should be something like localhost: 8080 / Calculate_2 / index.jsp, then when you click on the button, the browser will submit the form to the address localhost: 8080 / Calculator_2 / .... You can see the request sent by pressing the button using dev tools in the browser. Your code is correct, I just think that you are opening the page from the wrong place - Mikhail Chibel
  • What is the application server used? What is the development environment? In most cases, the address in the browser should be http://localhost:8080/Calculate_2/CalculateServlet . Didn't set Context root to a value other than the default? Then the address will be http://localhost:8080/Ваш_context_root/CalculateServlet In addition, as practice has shown, some use embedded servers, where the contextpath is formed differently. The address may also be http://localhost:8080/CalculateServlet . (Instead of port 8080, substitute your own) - Sergey

0