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.