Jsp learning --- use jsp and JavaBean to implement ultra-simple web Calculator

Source: Internet
Author: User

I. the requirement is as follows: use jsp to implement an ultra-simple web calculator. II. implementation 2. code Calculator. java copy code package com. amos. model; import java. math. bigDecimal;/*** @ ClassName: Calculator * @ Description: Calculator * @ author: amosli * @ email: amosli@infomorrow.com * @ date Mar 20,201 4 1:04:59 AM */public class Calculator {private String firstnum = "0"; private String secondnum = "0"; private char operator = '+ '; private String result; public String getResult () {r Eturn result;} public char getOperator () {return operator;} public void setOperator (char operator) {this. operator = operator;} public void setResult (String result) {this. result = result;} public String getFirstnum () {return firstnum;} public void setFirstnum (String firstnum) {this. firstnum = firstnum. trim ();} public String getSecondnum () {return secondnum;} public void setSecondnum (St Ring secondnum) {this. secondnum = secondnum. trim ();} public void calculate () {BigDecimal a = new BigDecimal (this. firstnum); BigDecimal B = new BigDecimal (this. secondnum); switch (this. operator) {case '+': this. result =. add (B ). toString (); break; case '-': this. result =. subtract (B ). toString (); break; case '*': this. result =. multiply (B ). toString (); break; case '/': if (B. doubleValue () = 0 ){ Throw new RuntimeException ("the divisor cannot be zero");} this. result =. divide (B, 10, BigDecimal. ROUND_HALF_DOWN ). toString (); break; default: break ;}} copy the code calculator. jsp copy Code <% @ page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN "" http://www.w3.org/TR/html4/loose.dtd "> <Html>

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.