首页 > 代码库 > project

project

sun公司推出了jsp技术后,同时还推荐了两种web应用程序的开发模式,一种是jsp+JavaBean模式,一种是Servlet+jsp+JavaBean模式。jsp+JavaBean模式适合开发业务逻辑不太复杂的web应用程序,这种模式下,JavaBean用于封装业务逻辑数据,jsp既负责处理用户请求,又显示数据。

Servlet+jsp+JavaBean模式(MVC)模式适合开发复杂的web应用,在这种模式下,Servlet负责处理用户请求,jsp负责数据显示,JavaBean负责封装数据。Servlet+jsp+JavaBean模式程序各个模块之间层次清晰,web开发推荐采用此种模式。

先看一个jsp+JavaBean模式的例子:编写计算器

 

 1 <%@page import="cn.itcast.CalculatorBean"%>
 2 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 3 
 4 
 5 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 6 <html>
 7   <head>
 8     <title>计算器</title>
 9   </head>
10   <body style="text-align:center;">
11       <jsp:useBean id="CalculatorBean" class="cn.itcast.CalculatorBean"></jsp:useBean>
12     <jsp:setProperty name="CalculatorBean" property="*"></jsp:setProperty>
13       <%
14           CalculatorBean.calculate();
15        %>
16       计算结果是:
17       <jsp:getProperty name="CalculatorBean" property="firstNum"/>
18       <jsp:getProperty name="CalculatorBean" property="operator"/>
19       <jsp:getProperty name="CalculatorBean" property="secondNum"/>
20       =
21       <jsp:getProperty name="CalculatorBean" property="result"/>
22       <br/><br/>
23 
24       <form action="/jsp/3.jsp" method="post">
25         <table border="1" width="50%">
26             <tr>
27                 <td colspan="2">一个简单计算器</td>
28             </tr>
29 
30             <tr>
31                 <td>第一个参数</td>
32                 <td>
33                     <input type="text" name="firstNum">
34                 </td>
35             </tr>
36 
37             <tr>
38                 <td>运算符</td>
39                 <td>
40                     <select name="operator">
41                         <option value="http://www.mamicode.com/+">+</option>
42                         <option value="http://www.mamicode.com/-">-</option>
43                         <option value="http://www.mamicode.com/*">*</option>
44                         <option value="http://www.mamicode.com/">/</option>
45                     </select>
46                 </td>
47             </tr>
48 
49             <tr>
50                 <td>第二个参数</td>
51                 <td>
52                     <input type="text" name="secondNum">
53                 </td>
54             </tr>
55 
56             <tr>
57                 <td colspan="2">
58                     <input type="submit" value="http://www.mamicode.com/计算结果">
59                 </td>
60             </tr>
61         </table>
62     </form>
63   </body>
64 </html>
 1 package cn.itcast;
 2 import java.math.BigDecimal;
 3 public class CalculatorBean {
 4     private double firstNum ;
 5     private double secondNum ;
 6     //注意:这里必须给默认值,不然在jsp中的CalculatorBean.calculate()会出现异常
 7     private char operator = ‘+‘;
 8     private double result ;
 9 
10     public void calculate(){
11         switch (this.operator) {
12             case ‘+‘:{
13                 this.result = this.firstNum + this.secondNum;
14                 break;
15             }
16             case ‘-‘:{
17                 this.result = this.firstNum - this.secondNum;
18                 break;
19             }
20             case ‘*‘:{
21                 this.result = this.firstNum * this.secondNum;
22                 break;
23             }
24             case ‘/‘:{
25                 if(this.secondNum == 0){
26                     throw new RuntimeException("除数不能为0");
27                 }
28                 this.result = this.firstNum / this.secondNum;
29                 //需要四舍五入,向上进位模式,保留两位小数
30                 this.result = new BigDecimal(this.result).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
31                 break;
32             }
33             default:
34                 throw new RuntimeException("对不起,传入的运算符非法");
35         }
36     }
37 
38 
39     public double getFirstNum() {
40         return firstNum;
41     }
42     public void setFirstNum(double firstNum) {
43         this.firstNum = firstNum;
44     }
45     public double getSecondNum() {
46         return secondNum;
47     }
48     public void setSecondNum(double secondNum) {
49         this.secondNum = secondNum;
50     }
51     public char getOperator() {
52         return operator;
53     }
54     public void setOperator(char operator) {
55         this.operator = operator;
56     }
57     public double getResult() {
58         return result;
59     }
60     public void setResult(double result) {
61         this.result = result;
62     }
63 
64 
65 }

 

project