Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Generally you should abstract your business logic from Servlet container details. You can mock ServletRequest using <a href="http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/mock/web/MockHttpServletRequest.html" rel="nofollow">Spring test package</a>, but it would be a bad idea to simulate Servlet container. So, you should either run system tests on a real container or move your logic from servlet into a separate bean and test it in isolation.</p> <pre><code> public class Calculator { public Integer calculate(Integer left, Integer right, String op) { Integer result = 0; if ("add".equals(op)) result = this.opAdd(left, right); if ("subtract".equals(op)) result = this.opSub(left, right); if ("multiply".equals(op)) result = this.opMul(left, right); if ("power".equals(op)) result = this.opPow(left, right); if ("divide".equals(op)) result = this.opDiv(left, right); if ("modulo".equals(op)) result = this.opMod(left, right); return result; } } public class CalculatorServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Integer left = Integer.valueOf(request.getParameter("left")); Integer right = Integer.valueOf(request.getParameter("right")); String op = request.getParameter("operator"); Integer result = calculator.calculate(left, right, op); request.setAttribute("result", result); // It'll be available as ${sum}. request.getRequestDispatcher("index.jsp").forward(request, response); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload