Home > Software engineering >  how to loop over values in servlets and display them in them table?
how to loop over values in servlets and display them in them table?

Time:02-14

I am writing a servlet calculator using doPost method of servlet container.

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //show result page

        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>SIMPLE CALCULATOR<br><br><br></head>");
        out.println("<body>");
        out.println("<form method = 'post' action = 'calc'>");
        out.println("enter the first number:<br>");
        out.println("<input type = 'text' name='number1'><br><br>");
        out.println("enter the second number:<br>");
        out.println("<input type = 'text' name='number2'><br><br>");
        out.println("enter the operation:<br><br>");
        out.println("<input type ='radio' name = 'op' value = ' '>add<br>");
        out.println("<input type = 'radio' name = 'op' value = '-'>sub<br>");
        out.println("<input type = 'radio' name = 'op' value = '*'>mul<br>");
        out.println("<input type = 'radio' name = 'op' value = '/'>div<br><br>");
        out.println("<input type = 'submit' name = 'result' value = 'submit'><br>");
        out.println("</body>");
        out.println("</html>");
        int a1= Integer.parseInt(request.getParameter("number1"));
        int a2= Integer.parseInt(request.getParameter("number2"));
        String operation = request.getParameter("op");
        int result = 0;
        if(operation.equals(" "))
        {
            result = a1   a2;
            out.println("The result of "   a1   " "   operation   " "  a2   " = "  result);
        }
        if(operation.equals("-"))
        {
            result = a1 - a2;
            out.println("The result of "   a1   " "   operation   " "   a2   " = "  result);
        }
        if(operation.equals("*"))
        {
            result = a1 * a2;
            out.println("The result of "   a1   " "   operation   " "   a2   " = "  result);
        }if(operation.equals("/"))
        {
            result = a1 / a2;
            out.println("The result of "   a1   operation   a2   " = "  result);
        }

        out.println("<table border=1>");
        out.println("<tr>");
        out.println("<th>first</th>");
        out.println("<th>operation</th>");
        out.println("<th>second</th>");
        out.println("<th>result</th>");
        out.println("</tr>");
        out.println("<tr>");
        out.println("<td>"   a1   "</td>");
        out.println("<td>"   operation   "</td>");
        out.println("<td>"   a2   "</td>");
        out.println("<td>"   result   "</td>");
        out.println("</tr>");
        out.println("</table>");
        
        out.flush();
    }

I wanted to display the values and operations and results everytime I do calculation and click the submit button. What I am now seeing is that only the first calculation values are displayed and when I do the second one It changes the previous one and displays itself. Expected output is enter image description here

I am now seeing enter image description here Anyone has any help?

CodePudding user response:

My solution would be to use a list of strings to store the history. If you want a better solution, you could write a class that has first, second, and operator so that you can retrieve each property. I store the list in the session so that it can be accessed by the JSP page. I'm just writing a minimal solution. It's up to you to rewrite it to meet your requirements.

JSP page

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<form action="ComputeServlet" method="post">
<input type="text" name="firstNumber" size="5"> 
<select name="operator">
  <option value=" "> </option>
  <option value=" ">-</option>
  <option value=" ">*</option>
  <option value=" ">/</option>
</select>
<input type="text" name="secondNumber" size="5"> 
<input type="submit" name="submit" value="submit">

<c:forEach var="problem" items="${history}">
  <p>${problem}</p>
</c:forEach>
<input type="submit" name="clear" value="clear">
</form>

Servlet

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
    
  if (request.getParameter("clear")!=null){
    List<String> history = (List<String>) request.getSession().getAttribute("history");
    if (history == null){
      history = new ArrayList<>();
      request.getSession().setAttribute("history", history);
    } else {
      history.clear();
    }     
  } else if (request.getParameter("submit")!=null){
     int first = Integer.parseInt(request.getParameter("firstNumber"));
     int second = Integer.parseInt(request.getParameter("secondNumber"));
     String op = request.getParameter("operator");
     int result = 0;
     switch(op){
       case " ": result = first second; break;
       case "*": result = first*second; break;
       case "/": result = first/second; break;
       case "-": result = first-second; break;
      }
    List<String> history = (List<String>) request.getSession().getAttribute("history");
    if (history == null){
      history = new ArrayList<>();
      request.getSession().setAttribute("history", history);
    }
    history.add(""   first   op   second   "="   result);
  }
  getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
}

Output

<form action="ComputeServlet" method="post">
<input type="text" name="firstNumber" size="5"> 
<select name="operator">
  <option value=" "> </option>
  <option value=" ">-</option>
  <option value=" ">*</option>
  <option value=" ">/</option>
</select>
<input type="text" name="secondNumber" size="5"> 
<input type="submit" name="submit" value="submit">

  <p>4 5=9</p>
  <p>2 6=8</p>
  <p>3 2=5</p>
  <p>3 2=5</p>

<input type="submit" name="clear" value="clear">
</form>

enter image description here

  • Related