1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP ‘Calculator.jsp‘ starting page</title> 13 14 15 </head> 16 17 <body> 18 19 <jsp:useBean id="Calculator" class="cn.itcast.domain.Calculator"></jsp:useBean> 20 <jsp:setProperty property="*" name="Calculator"/> 21 22 <% 23 try{ 24 Calculator.calculate(); 25 }catch(Exception e){ 26 out.write(e.getMessage()); 27 } 28 %> 29 30 <br/>--------------------------------------------<br/> 31 计算的结果是: 32 <jsp:getProperty name="Calculator" property="firstnum"/> 33 <jsp:getProperty name="Calculator" property="operator"/> 34 <jsp:getProperty name="Calculator" property="secondnum"/> 35 = 36 <jsp:getProperty name="Calculator" property="result"/> 37 <br/>--------------------------------------------<br/> 38 <br/> 39 <form action="/JSPDemo/Calculator.jsp" method="post" style="text-align:center;"> 40 <table width="40%" border="1"> 41 <tr> 42 <td colspan="2">简单的计算器</td> 43 </tr> 44 <tr> 45 <td>第一个参数</td> 46 <td> 47 <input type="text" name="firstnum" /> 48 </td> 49 </tr> 50 <tr> 51 <td>操作符</td> 52 <td> 53 <select name="operator" > 54 <option value="+">+</option> 55 <option value="-">-</option> 56 <option value="*">*</option> 57 <option value="/">/</option> 58 </select> 59 </td> 60 </tr> 61 <tr> 62 <td>第二个参数</td> 63 <td> 64 <input type="text" name="secondnum" /> 65 </td> 66 </tr> 67 <tr> 68 <td colspan="2"> 69 <input type="submit" value="计算" /> 70 </td> 71 </tr> 72 </table> 73 </form> 74 75 </body> 76 </html>
Calculator.jsp
1 package cn.itcast.domain; 2 3 4 import java.math.BigDecimal; 5 6 //封装计算器数据的bean 7 public class Calculator { 8 9 private String firstnum = "0"; 10 private String secondnum = "0"; 11 private char operator = ‘+‘; 12 private String result; 13 14 public void calculate(){ 15 BigDecimal first = new BigDecimal(this.firstnum); 16 BigDecimal second = new BigDecimal(this.secondnum); 17 18 switch(this.operator){ 19 case ‘+‘:{ 20 this.result = first.add(second).toString(); 21 break; 22 } 23 case ‘-‘:{ 24 this.result = first.subtract(second).toString(); 25 break; 26 } 27 case ‘*‘:{ 28 this.result = first.multiply(second).toString(); 29 break; 30 } 31 case ‘/‘:{ 32 if(second.doubleValue()==0){ 33 throw new RuntimeException("被除数不能为0"); 34 } 35 this.result = first.divide(second,20,BigDecimal.ROUND_HALF_UP).toString(); 36 break; 37 } 38 39 default: 40 throw new RuntimeException("运算符只能+-*/"); 41 } 42 43 } 44 45 public String getFirstnum() { 46 return firstnum; 47 } 48 49 public void setFirstnum(String firstnum) { 50 this.firstnum = firstnum; 51 } 52 53 public String getSecondnum() { 54 return secondnum; 55 } 56 57 public void setSecondnum(String secondnum) { 58 this.secondnum = secondnum; 59 } 60 61 public char getOperator() { 62 return operator; 63 } 64 65 public void setOperator(char operator) { 66 this.operator = operator; 67 } 68 69 public String getResult() { 70 return result; 71 } 72 73 public void setResult(String result) { 74 this.result = result; 75 } 76 }
Calculator
jsp开发模式和web计算器案例
时间: 2024-11-18 05:25:12