Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing calculation result to JSP page
    primarykey
    data
    text
    <p>Index.jsp takes two input number and after submit the request go to Operation.java. It has a radio button to select Operation. Both inputs and radio button are submitted to Operation.java.</p> <pre><code>&lt;body&gt; &lt;h1&gt;Easy way to do fast operation&lt;/h1&gt; &lt;form action="Operation"&gt; First number::&lt;input type="text" name="firstno"&gt;&lt;/input&gt;&lt;/br&gt;&lt;/br&gt; Second number::&lt;input type="text" name="Secondno"&gt;&lt;/input&gt;&lt;/br&gt;&lt;/br&gt; &lt;input type="radio" name="option" value="add"&gt;Add&lt;/input&gt; &lt;input type="radio" name="option" value="substract"&gt;Subtract&lt;/input&gt; &lt;input type="radio" name="option" value="mul"&gt;Multiply&lt;/input&gt; &lt;input type="radio" name="option" value="divide"&gt;Divide&lt;/input&gt; &lt;/br&gt;&lt;/br&gt; &lt;input type="submit" value="submit"/&gt; &lt;/form&gt; &lt;%if(request.getAttribute("res")!=null){%&gt; The result is ::${res} &lt;%}%&gt; &lt;/body&gt; </code></pre> <p>Operation.java(Servlet) takes value from input button and do calculation based on the radio button click. It will calculate the result.</p> <pre><code>int result1=0; int n1=Integer.parseInt(request.getParameter("firstno")); int n2=Integer.parseInt(request.getParameter("Secondno")); String radio=request.getParameter("option"); if(radio.equals("add")) { result1=n1+n2; } else if(radio.equals("substract")) { result1=n1-n2; } else if(radio.equals("mul")) { result1=n1*n2; } request.setAttribute("res", result1); RequestDispatcher requestDispatcher = request.getRequestDispatcher("index.jsp"); requestDispatcher.forward(request, response); </code></pre> <p>After calculation I want to show the result on the index.jsp. How can I do that?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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