JSP web Calculator

Source: Internet
Author: User

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;


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.