A simple calculator is implemented based on JSP, and a calculator is implemented by jsp.

Source: Internet
Author: User

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.

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.