Using JSP to make an example of a super simple Web calculator sharing _jsp programming

Source: Internet
Author: User

The

Implements a simple calculator program that requires: implemented using the Jsp+javabean pattern. The
Project source code is as follows:
File: calculator.jsp

<%@ page language= "java" pageencoding= "UTF-8"%> <%@ page iserrorpage= "true"%> <%@ page errorpage= "Calcul" ator.jsp "%> <%@ taglib uri=" Http://java.sun.com/jsp/jstl/core "prefix=" C "%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  

  
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 = Secondnum; 
  This.result = result; } public void CalculAte () {if (operator!= null &&!operator.equals ("")) {Double A = new double (firstnum); 
      Double second = new Double (secondnum); 
      Char oper = operator.charat (0); 
        Switch (oper) {case ' + ': result = second + ""; 
      Break 
        Case '-': result = First-second + ""; 
      Break 
        Case ' * ': result = second + ""; 
      Break 
        Case '/': result = First/second + ""; 
      Break 
      Default:throw New RuntimeException ("Unknown operator!"); 

 } 
    } 
  } 
}

The final results are shown below:

Summary: The front is only a small exercise, in fact, there are many flaws in the data submitted after the failure to verify. Obviously Jsp+javabean mode is only suitable for simple calculation, if the function to be completed is more complex, this mode is not suitable. If you are dealing with some business in a JSP page, also makes the program chaotic unbearable, finally difficult to maintain, the above example business logic processing is directly in the JavaBean, the actual project, the business will not be so simple, often involved in some of the operation of the database, should be separated from all components, So later maintenance is also easier, and will allow programmers to feast for the eyes, if it is a logical level is not a very good project, maintenance really disgusting, let me crazy.

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.