The server does not display the answer when calculating

43 Views Asked by At

After running the program (calculator test sample), the result is not displayed. The Result line: ${requestScope.result} - does not change, although the result should be displayed instead of ${requestScope.result}. At the same time, the console in intellij idea displays the answer. That is, when you press the "calculate" button, there are no changes on the server, and the console displays the correct answer. When dividing by zero, the server displays the message “You cannot divide by zero” specified in the method. What could be the problem? My Servlet:

import jakarta.servlet.RequestDispatcher;
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 java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/calculate")
public class CalculatorServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws     ServletException, IOException {
        RequestDispatcher dispatcher = req.getRequestDispatcher("calculatorServlet.jsp");
        dispatcher.forward(req, resp);
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException,     ServletException {
        resp.setContentType("text/html");
        PrintWriter writer;
        writer = resp.getWriter();
        String operation = req.getParameter("operation");
        double firstOperand = Double.parseDouble(req.getParameter("firstOperand"));
        double secondOperand = Double.parseDouble(req.getParameter("secondOperand"));
        double result;
        switch (operation) {
            case "add":
                result = firstOperand + secondOperand;
                break;
            case "subtraction":
                result = firstOperand - secondOperand;
                break;
            case "multiply":
                result = firstOperand * secondOperand;
                break;
            case "divide":
                if (secondOperand != 0) {
                    result = firstOperand / secondOperand;
                } else {
                    writer.println("You cannot divide by zero");
                    return;
                }
                break;
            default:
                writer.println("I don't know its operation!");
                return;
        }
        req.setAttribute("result", result);
        RequestDispatcher dispatcher = req.getRequestDispatcher("/calculatorServlet.jsp");
        dispatcher.forward(req, resp);
        System.out.println("Result: " + result);
    }
}

my jsp:

<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<html>
<head>
<title>Calculate Page</title>
</head>
<body>
<h1>Calculate Page</h1>
<form action="calculate" method="post">
<label for="firstOperand">Enter number 1: </label>
<input type="text" id="firstOperand" name="firstOperand" required>
<label for="operation">Choose operation: </label>
<select id="operation" name="operation">
<option value="add">Addition</option>
<option value="subtraction">Subtraction</option>
<option value="multiply">Multiplication</option>
<option value="divide">Division</option>
</select>
<label for="secondOperand">Enter number 2: </label>
<input type="text" id="secondOperand" name="secondOperand" required>
<button type="submit">Calculate</button>
</form>
<div>
 <c:if test="${not empty requestScope.result}">
 <p>Result: ${requestScope.result}</p>
</c:if>
</div>
</body>
</html>

enter image description here what could be the reasons?

1

There are 1 best solutions below

0
PavelPavlik On

The reason was in the web.xml settings