A simple calculator for JSP exercises (using Jsp+javabean mode)

Source: Internet
Author: User



Implement a simple calculator program that requires: Use the Jsp+javabean mode implementation.



The project source code is as follows:



Files: 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"%>
<! DOCTYPE HTML PUBLIC "-// W3C // DTD HTML 4.01 Transitional // EN">
<html>
  <head>
    <title> Simple computer </ title>
  </ head>
  
  <body>
  <%-Create Calculator object-%>
  <jsp: useBean id = "cal" class = "cn.zq.domain.Calculator" />
  <%-Set value Wildcard * means set all attributes-%>
  <jsp: setProperty property = "*" name = "cal" />
  The
  The
  <%-
  		Calculation
  -%>
  <c: if test = "$ {empty pageContext.exception}">
  <%
  cal.calculate ();
  %>
  </ c: if>
  The
  The
  <hr />
  <p> Calculation result: $ {cal.firstNum} $ {cal.operator} $ {cal.secondNum} = $ {cal.result} </ p>
  <hr />
  The
  <%-
  Build url
  -%>
  <c: url var = "formUrl" value = "/ calculator.jsp" />
  <form action = "$ {formUrl}" method = "post">
<table border = "1" cellpadding = "2">
<tr>
<td colspan = "2" align = "center"> My calculator </ td>
</ tr>
<tr>
<td> First parameter: </ td>
<td> <input type = "text" name = "firstNum" /> </ td>
</ tr>
<tr>
<td> Operator: </ td>
<td>
<select name = "operator">
<option value = "+"> + </ option>
<option value = "-">-</ option>
<option value = "*"> * </ option>
<option value = "/"> / </ option>
</ select>
</ td>
</ tr>
<tr>
<td> Second parameter: </ td>
<td>
<input type = "text" name = "secondNum">
</ td>
</ tr>
<tr>
<td colspan = "2">
<input type = "submit" value = "Calculation" />
</ td>
</ tr>
</ table>
  </ form>
  </ body>
</ html>



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;
}
The
public void calculate () {
if (operator! = null &&! operator.equals ("")) {
double first = new Double (firstNum);
double second = new Double (secondNum);
char oper = operator.charAt (0);
switch (oper) {
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:


Summary: The previous is just a small exercise, in fact, there are still many defects, and the data has not been verified after submission. Obviously, the jsp + javabean mode is only suitable for simple calculations. If the function to be completed is more complicated, this mode is not suitable. If you do some business processing on the jsp page, it also makes the program chaotic, and finally it is difficult to maintain. The processing of the business logic in the above example is done directly in javabean. In actual projects, business is not so simple, often involving some For the operation of the database, the various components should be separated, so that it will be easier to maintain in the future, and it will also make the programmers enjoyable. If it is a project with a logical hierarchy that is not very good, the maintenance is really disgusting , Driving me crazy.


Simple calculator for JSP practice (using jsp + javabean mode)

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.