This article illustrates a method of implementing a simple calculator based on JSP. Share to everyone for your reference. The implementation methods are as follows:
<%@ page language= "java" import= "java.util.*" pageencoding= "GB18030"%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>
<!--user Submit validation-->
<script type= "Text/javascript" language= "JavaScript" >
<!--
function Checknum () {
if (form1.num1.value== "") {
Window.alert ("Num1 value cannot be empty!!") Haha ");
return false;
}
Judge Num1 is not a number
if (Math.Round (form1.num1.value)!= (Form1.num1.value)) {
Window.alert ("Num1 not an integer")
return false;
}
if (form1.num2.value== "") {
Window.alert ("num2 value cannot be empty!!") Haha ");
return false;
}
Judge Num2 is not a number
if (Math.Round (form1.num2.value)!= (Form1.num2.value)) {
Window.alert ("num2 not an integer")
return false;
}
}
-->
</script>
<body>
<form name = "Form1" action= "result.jsp" method = "POST" >
<input type= "text" name = "NUM1" ></input><br>
<select name= "Flag" >
<option value=+>+</option>
<option value=->-</option>
<option value=*>*</option>
<option value=/>/</option>
</select><br>
<input type= "text" name= "num2"/></input><br>
<input type= "Submit" value= "submitted" onclick= "return Checknum ();" ></input>
</form>
</body>
Result.jsp for displaying results
Copy Code code as follows:
<%@ page language= "java" import= "java.util.*" pageencoding= "GB18030"%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<body>
<%
String num1=request.getparameter ("Num1");
String num2 = Request.getparameter ("num2");
String flag = Request.getparameter ("flag");
int S_num1=integer.parseint (NUM1);
int S_num2=integer.parseint (NUM2);
int result=0;
if (flag.equals ("+")) {
Add
result=s_num1+s_num2;
}else if (flag.equals ("-")) {
Reducing
result=s_num1-s_num2;
}else if (flag.equals ("/")) {
result=s_num1/s_num2;
Except
}else{
By
result=s_num1*s_num2;
}
OUT.PRINTLN ("The result is:" +result);
%>
</body>
I hope this article will help you with the JSP program design.