JSP web Calculator
(1) The code for implementing the Web calculator in jsp is as follows: there are only two jsp pages.
MyCal. jsp is as follows:
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %>My JSP 'mycal. jsp 'starting page
<Script language = "javascript">
</Script>
MyResult. jsp is as follows:
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> My JSP 'myresult. jsp 'starting page
<% // The number of requests received in step 1 is String s_num1 = request. getParameter ("num1"); // The number of requests received in step 1 is String s_num2 = request. getParameter ("num2"); // The String flag = request is received in step 1. getParameter ("flag"); // calculate int num1 = Integer in step 1. parseInt (s_num1); int num2 = Integer. parseInt (s_num2); int result = 0; if (flag. equals ("+") {result = num1 + num2;} else if (flag. equals ("-") {result = num1-num2;} else if (flag. equals ("*") {result = num1 * num2;} else if (flag. equals ("/") {result = num1/num2;} // step 3 out. println ("result:" + result); %>
(2) Although the process is simple, there are several points worth learning:
How to determine whether the input data is a number: use Math. round (form1.num1. value )! = Form1.num1. value to judge;
How to get the operation value: Set the flag implementation of the name attribute;