A simple calculator is implemented based on JSP, and a calculator is implemented by jsp.
This example describes how to implement a simple calculator Based on JSP. Share it with you for your reference. The specific implementation method is as follows:
Index. jsp
Copy codeThe Code is as follows: <% @ page language = "java" import = "java. util. *" pageEncoding = "GB18030" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>
<Html>
<Head>
<! -- The user submits the verification -->
<Script type = "text/javascript" language = "javascript">
<! --
Function checkNum (){
If (form1.num1. value = ""){
Window. alert ("The num1 value cannot be blank !! Haha ");
Return false;
}
// Judge whether num1 is a number.
If (Math. round (form1.num1. value )! = (Form1.num1. value )){
Window. alert ("num1 is not an integer ")
Return false;
}
If (form1.num2. value = ""){
Window. alert ("The num2 value cannot be blank !! Haha ");
Return false;
}
// Judge whether num2 is a number.
If (Math. round (form1.num2. value )! = (Form1.num2. value )){
Window. alert ("num2 is not an integer ")
Return false;
}
}
-->
</Script>
</Head>
<H1> my calculator <Hr>
<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 = "submit" onclick = "return checkNum ();"> </input>
</Form>
<Hr>
</Body>
</Html>
Result. jsp is used to display results
Copy codeThe Code is 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">
<Html>
<Head>
</Head>
<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 ("-")){
// Subtract
Result = s_num1-s_num2;
} Else if (flag. equals ("/")){
Result = s_num1/s_num2;
// Division
} Else {
// Multiplication
Result = s_num1 * s_num2;
}
Out. println ("result:" + result );
%>
</Body>
</Html>
I hope this article will help you with jsp program design.