title: Perform simple arithmetic operation and show the result on the same jsp page
body: <p>I'm trying to wrap my head around servlets and JSP and I got stuck when implementing a simple calculator.</p>
  
  <p>Basically, I have two input fields, operator selection field and the submit button.</p>
  
  <p>When I click on the submit button I need to perform the chosen arithmetic operation on two values from input elements and show the result on the same page.</p>
  
  <p>Here's what I have:</p>
  
  <pre><code>&lt;!-- hello.jsp page --&gt;
  
  &lt;form action="hello.jsp" id="calc-form"&gt;
      &lt;input type="number" name="num1" required&gt;
      &lt;select id="opers" name="oper"&gt;
          &lt;option&gt;+&lt;/option&gt;
          &lt;option&gt;-&lt;/option&gt;
          &lt;option&gt;*&lt;/option&gt;
          &lt;option&gt;/&lt;/option&gt;
      &lt;/select&gt;
      &lt;input type="number" name="num2" required&gt;
      &lt;input type="submit" value="Calculate"&gt;
  &lt;/form&gt;
  &lt;h2&gt;The result is: ${result}&lt;/h2&gt;
  </code></pre>
  
  <p>my <code>doGet</code> method in <code>hello</code> servlet:</p>
  
  <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      response.setContentType("text/html");
  
      System.out.println("Hello#doGet");
  
      String strNum1 = request.getParameter("num1");
      String strNum2 = request.getParameter("num2");
      String oper = request.getParameter("oper");
  
      double a, b, result = 0;
  
      if(validateNum(strNum1) &amp;&amp; validateNum(strNum2) &amp;&amp; validateOper(oper)) {
  
          try {           
  
              a = Double.parseDouble(request.getParameter("num1"));
              b = Double.parseDouble(request.getParameter("num2"));
  
              switch(oper) {
              case "+":
                  result = a + b;
                  break;
              case "-":
                  result = a - b;
                  break;
              case "*":
                  result = a * b;
                  break;
              case "/":
                  if (b == 0) {
                      throw new ArithmeticException("Division by zero is not allowed");
                  } else {
                      result = a / b;
                  }
              }
          } catch(NumberFormatException | ArithmeticException e) {
              // handle the exception somehow
          }
          request.setAttribute("result", result);
  
      }
  
      RequestDispatcher dispatcher = request.getRequestDispatcher("/hello.jsp");
      dispatcher.forward(request, response);
  }
  </code></pre>
  
  <p>So, when I go to <code>http://localhost:8080/test2/hello</code>, enter the numbers in input elements and press submit, I get redirected to address that looks pretty much like this:
  <code>http://localhost:8080/test2/hello.jsp?num1=4&amp;oper=*&amp;num2=4</code></p>
  
  <p>however, I don't get the result.</p>
  
  <p>Could you please tell me what I'm doing wrong here?</p>