title: JSP - Get Add result in the same form/same page
body: <p>I am trying to figure out how to add two nubers and have the result on the same page using Java/jsp (in this case - in the "result" area in the jsp. I tried with session attribute, but there is still something wrong with my code...
  I do not want to use javascript etc, but "pure java" only :) </p>
  
  <p>Could you help:</p>
  
  <p>Servlet: </p>
  
  <pre><code>package wprowadzenie;
  
  import java.io.IOException;
  
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  @WebServlet("/AddServlet.html")
  public class AddServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  
  private Integer getInt(String s) {
      try {
          Integer a = new Integer(s);
          return a;
      } catch (NumberFormatException e) {
          return null;
      }
  }
  
  public void serviceRequest(HttpServletRequest request,
          HttpServletResponse response) throws ServletException, IOException     {
  
      RequestDispatcher rd = request.getRequestDispatcher("/add.jsp");
      rd.forward(request, response);
  
      String no1 = request.getParameter("a");
      String no2 = request.getParameter("b");
      Integer one = getInt(no1);
      Integer two = getInt(no2);
  
      if (one != null &amp;&amp; two != null) {
          int result = one + two;
          System.out.println(result);
          HttpSession session = request.getSession();
          session.setAttribute("result", result);
  
      }
  
  }
  
  protected void doGet(HttpServletRequest request,
          HttpServletResponse response) throws ServletException, IOException     {
  
      System.out.println("get");
      serviceRequest(request, response);
  }
  
  protected void doPost(HttpServletRequest request,
          HttpServletResponse response) throws ServletException, IOException {
  
      System.out.println("post");
      serviceRequest(request, response);
  }
  
  }
  </code></pre>
  
  <p>JSP:</p>
  
  <pre><code>&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;form action="AddServlet.html" method="get"&gt;
      &lt;table&gt;
          &lt;tr&gt;
              &lt;td&gt;B&lt;/td&gt;
              &lt;td&gt;&lt;input type="text" name="a" /&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;A&lt;/td&gt;
              &lt;td&gt;&lt;input type="text" name="b" /&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Result:&lt;/td&gt;
              &lt;td&gt;&lt;input type="text" name="result" disabled="disabled"/&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
      &lt;input type="submit" value="add"/&gt;
  
  
  
  &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>