A method of implementing a simple calculator based on JSP _jsp programming

Source: Internet
Author: User

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:

index.jsp

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+ "/";
%>
<!--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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.