Use JSP to create an ultra-simple web calculator instance for sharing

Source: Internet
Author: User
This article mainly introduces an example of using JSP to create an ultra-simple web calculator. The method in this article uses the JavaBean Component. If you need a friend, you can refer to the following to implement a simple calculator program. Requirements: use jsp + javabean mode.
The source code of the project is as follows:
File: calculator. jsp

<% @ Page language = "java" pageEncoding = "UTF-8" %> <% @ page isErrorPage = "true" %> <% @ page errorPage = "calculator. jsp "%> <% @ taglib uri =" http://java.sun.com/jsp/jstl/core "prefix =" c "%>      Simple Computer       <% -- Create a Calculator object -- %>
 <% -- Set Value wildcard * Indicates setting all attributes -- %>
 <% -- Calculate -- %>
 
  
<% Cal. calculate (); %>
            
    

Calculation result: $ {cal. firstNum }$ {cal. operator }$ {cal. secondNum }=$ {cal. result}

<% -- Construct url -- %>

The javabean code used in this article is as follows:

Package cn. zq. domain; public class Calculator {private String firstNum; private String operator; private String secondNum; private String result; public String getFirstNum () {return firstNum;} public void setFirstNum (String firstNum) {this. firstNum = firstNum;} public String getOperator () {return operator;} public void setOperator (String operator) {this. operator = operator;} public String GetSecondNum () {return secondNum;} public void setSecondNum (String secondNum) {this. secondNum = secondNum;} public String getResult () {return result;} public void setResult (String result) {this. result = result;} public Calculator () {} public Calculator (String firstNum, String operator, String secondNum, String result) {this. firstNum = firstNum; this. operator = operator; this. secondNum = s EcondNum; this. result = result;} public void calculate () {if (operator! = Null &&! Operator. equals ("") {double first = new Double (firstNum); double second = new Double (secondNum); char character = operator. charAt (0); switch (seconds) {case '+': result = first + second + ""; break; case '-': result = first-second + ""; break; case '*': result = first * second + ""; break; case '/': result = first/second + ""; break; default: throw new RuntimeException ("unknown operator! ");}}}}

The final result is as follows:

For more information about how to use JSP to create an ultra-simple web page calculator, see the PHP Chinese website!

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.