Index.jsp 接受两个输入数字,提交请求后转到 Operation.java。它有一个单选按钮来选择操作。输入和单选按钮都提交给 Operation.java。
<body>
<h1>Easy way to do fast operation</h1>
<form action="Operation">
First number::<input type="text" name="firstno"></input></br></br>
Second number::<input type="text" name="Secondno"></input></br></br>
<input type="radio" name="option" value="add">Add</input>
<input type="radio" name="option" value="substract">Subtract</input>
<input type="radio" name="option" value="mul">Multiply</input>
<input type="radio" name="option" value="divide">Divide</input>
</br></br>
<input type="submit" value="submit"/>
</form>
<%if(request.getAttribute("res")!=null){%>
The result is ::${res}
<%}%>
</body>
Operation.java(Servlet) 从输入按钮获取值并根据单选按钮单击进行计算。它将计算结果。
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);
计算后我想在 index.jsp 上显示结果。我怎样才能做到这一点?