从制造到创造
软件工程师成长之路
posts - 249,  comments - 96,  trackbacks - 0
JSP动态网页设计之JSP与JavaBean。编程环境:Java 5.0.8+eclipse 3.2 + MyEclipse 5.0 + Tomcat 5.5.17

JavaBean01.gif

JavaBean02.gif
JavaBean03.gif

JavaBean04.gif

JavaBean05.gif

JavaBean06.gif

JavaBean07.gif

好,学完理论,下面编写代码:

打开eclipse,新建一个MyEclipse下面J2EE类的web项目,如图:

snap175.gif

项目名为:JspJiLinCh06,今天所有代码的树型图如下:

snap169.gif

新建一个com.coderdream.bean 包,然后新建一个SampleBean1类,代码如下:

 1 package com.coderdream.bean;
 2 
 3 public class SampleBean1 {
 4     
 5     private String str;
 6 
 7     public SampleBean1() {
 8         // TODO 自动生成构造函数存根
 9     }
10     
11     public String getStr() {
12         return str;
13     }
14 
15     public void setStr(String str) {
16         this.str = str;
17     }
18     
19 }


JavaBean08.gif

然后再新建一个sample1.jsp页面,代码如下:

 1<%@ page language="java" contentType="text/html; charset=GB2312"%>
 2<jsp:useBean id="smBean" class="com.coderdream.bean.SampleBean1"></jsp:useBean>
 3<html>
 4    <head>
 5        <title>一个简单的JavaBean程序</title>
 6    </head>
 7    <body>
 8        <center>
 9            <%
10                smBean.setStr("这是我的第一个JavaBean程序!");
11            %>
12            <%=smBean.getStr()%>
13        </center>
14    </body>
15</html>

        将项目发布后,打开Tomcat 服务器,输入网址,结果为:

snap176.gif



JavaBean09.gif

JavaBean10.gif

JavaBean11.gif

JavaBean12.gif

下面来看看如何使用的两个标记:分别建立SampleBean2类和两个页面文件sample2.html和sample2.jsp,代码如下:

SampleBean2.java
 1 package com.coderdream.bean;
 2 
 3 public class SampleBean2 {
 4     
 5     private String id;
 6     private String age;
 7     
 8     public SampleBean2() {
 9         // TODO 自动生成构造函数存根
10     }
11 
12     public String getAge() {
13         return age;
14     }
15 
16     public void setAge(String age) {
17         this.age = age;
18     }
19 
20     public String getId() {
21         return id;
22     }
23 
24     public void setId(String id) {
25         this.id = id;
26     }
27 
28 }

sample2.html
 1<html>
 2  <head>
 3    <title>使用&lt;jsp:Property&gt&lt;jsp:setProperty&gt标记</title>
 4   <meta http-equiv="content-type" content="text/html; charset=GB2312">
 5  </head>
 6
 7  <body>
 8    <form name="form1" action="sample2.jsp" method="post">
 9      <table align="center" border="0">
10        <tr>
11          <td>编号:</td>
12          <td><input type="text" name="id" ></td>
13        </tr>
14        <tr>
15          <td>年龄:</td>
16          <td><input type="text" name="age"></td>
17        </tr> 
18        <tr>
19          <td><input type="submit" value="提交"></td>
20          <td><input type="reset" value="重填"></td>
21        </tr>
22      </table>
23    </form>
24  </body>
25</html>
26

sample2.jsp

<%@ page language="java" contentType="text/html; charset=GB2312"%>
<jsp:useBean id="splBean" class="com.coderdream.bean.SampleBean2"></jsp:useBean>
<html>
    
<head>
        
<title>My JSP 'sample1.jsp' starting page</title>
    
</head>
    
<body>
        
<!--  jsp:setProperty name="splBean" property="*"/-->
        
        
<!--  jsp:setProperty name="splBean" property="id"/-->
        
<!--  jsp:setProperty name="splBean" property="age"/-->
        
        
<!--  jsp:setProperty name="splBean" property="id" param="id"/-->
        
<!--  jsp:setProperty name="splBean" property="age" param="age"/-->
        
        
<jsp:setProperty name="splBean" property="id" value="${param.id}"/>
        
<jsp:setProperty name="splBean" property="age" value="${param.age}"/>
        
        
<!--  jsp:setProperty name="splBean" property="id" value="001"/-->
        
<!--  jsp:setProperty name="splBean" property="age" value="29"/-->
        
        
<center>
            编号:
<jsp:getProperty name="splBean" property="id"/>
            年龄:
<jsp:getProperty name="splBean" property="age"/>
        
</center>
    
</body>
</html>


通过jsp代码可知,可用4种方式设置标记:

         <jsp:setProperty name="splBean" property="*"/>

        <jsp:setProperty name="splBean" property="id"/>
        
<jsp:setProperty name="splBean" property="age"/>

        <jsp:setProperty name="splBean" property="id" param="id"/>
        
<jsp:setProperty name="splBean" property="age" param="age"/>

        <jsp:setProperty name="splBean" property="id" value="${param.id}"/>
        
<jsp:setProperty name="splBean" property="age" value="${param.age}"/>

运行结果为:
         ①输入编号和姓名:
snap178.gif
      ② 返回结果:
snap179.gif


JavaBean13.gif
JavaBean14.gif

JavaBean16.gif

JavaBean15.gif




JavaBean17.gif

先建立一个JavaBean,JavaBean5.java

 1 package com.coderdream.bean;
 2 
 3 public class SampleBean4 {
 4 
 5     private String sumIncome = "";
 6 
 7     private String taxStart = "";
 8 
 9     private double tax = 0;
10 
11     public SampleBean4() {
12         // TODO 自动生成构造函数存根
13     }
14 
15     public String getSumIncome() {
16         return sumIncome;
17     }
18 
19     public void setSumIncome(String sumIncome) {
20         this.sumIncome = sumIncome;
21     }
22 
23     public double getTax() {
24         return tax;
25     }
26 
27     public void setTax(double tax) {
28         this.tax = tax;
29     }
30 
31     public String getTaxStart() {
32         return taxStart;
33     }
34 
35     public void setTaxStart(String taxStart) {
36         this.taxStart = taxStart;
37     }
38 
39     public void calculate() {
40         double totalMoney = Double.parseDouble(sumIncome);
41         double baseMoney = Double.parseDouble(taxStart);
42         double balance = totalMoney - baseMoney;
43 
44         System.out.println(totalMoney);
45         System.out.println(baseMoney);
46         
47         try {
48             if (balance < 0)
49                 tax = 0;
50             if (balance > 0 && balance <= 500)
51                 tax = balance * 0.05;
52             if (balance > 500 && balance <= 2000)
53                 tax = balance * 0.1 - 25;
54             if (balance > 2000 && balance <= 5000)
55                 tax = balance * 0.15 - 125;
56             if (balance > 5000 && balance <= 20000)
57                 tax = balance * 0.2 - 375;
58             if (balance > 20000 && balance <= 40000)
59                 tax = balance * 0.25 - 1375;
60             if (balance > 40000 && balance <= 60000)
61                 tax = balance * 0.3 - 3375;
62             if (balance > 60000 && balance <= 80000)
63                 tax = balance * 0.35 - 6375;
64             if (balance > 80000 && balance <= 100000)
65                 tax = balance * 0.4 - 10375;
66             if (balance > 100000)
67                 tax = balance * 0.45 - 15375;
68         } catch (Exception e) {
69             System.out.println(e.toString());
70         }
71     }
72 
73 }

然后建立两个测试页面:sample3.html和sample8.jsp

sample3.html

 1 <html>
 2     <head>
 3         <title>个人所得税计算器</title>
 4         <meta http-equiv="content-type" content="text/html; charset=GB2312">
 5     </head>
 6 
 7     <body>
 8         <form name="form1" method="post" action="sample8.jsp">
 9             <table width="358" height="140" border="0" cellpadding="0"
10                 cellspacing="0" bgcolor="#E6E6E6">
11                 <tr align="center">
12                     <td colspan="3">
13                         <strong>个人所得税计算器</strong>
14                     </td>
15                 </tr>
16                 <tr>
17                     <td colspan="2">
18                         当月个人总收入:
19                     </td>
20                     <td width="177">
21                         <input name="sumIncome" type="text">
22                     </td>
23                 </tr>
24                 <tr>
25                     <td colspan="2">
26                         当地个人所得税起征额:
27                     </td>
28                     <td>
29                         <input name="taxStart" type="text">
30                     </td>
31                 </tr>
32                 <tr align="center">
33                     <td width="161" align="right">
34                         <input type="submit" name="Submit" value="确定">
35                     </td>
36                     <td width="20"></td>
37                     <td align="left">
38                         <input type="reset" name="Reset" value="重填">
39                     </td>
40                 </tr>
41             </table>
42         </form>
43     </body>
44 </html>

sample8.jsp
 1 <%@ page language="java" contentType="text/html; charset=GB2312"%>
 2 <jsp:useBean id="splBean" class="com.coderdream.bean.SampleBean4" scope="request"></jsp:useBean>
 3 <html>
 4     <head>
 5         <title>个人所得税计算器</title>
 6     </head>
 7     <body>
 8         <%
 9             String sumIncome = request.getParameter("sumIncome");
10             String taxStart = request.getParameter("taxStart");
11                 
12             splBean.setSumIncome(sumIncome);
13             splBean.setTaxStart(taxStart);
14 
15             splBean.calculate();
16         %>
17         <center>
18             <p>
19                 当月应缴个人所得税为:
20                 <strong>
21                     <%
22                         out.println(splBean.getTax());
23                     %>
24                 </strong>
25             </p>
26         </center>
27     </body>
28 </html>

输出结果为:

snap182.gif

snap183.gif



最后,我们设计一个计算器,先建立一个JavaBean,然后新建一个测试页面:
JavaBean5.java
 1 package com.coderdream.bean;
 2 
 3 public class SampleBean5 {
 4 
 5     private String first;
 6     private String second;
 7     private double result;
 8     private String operator;
 9     
10     public SampleBean5() {
11         // TODO 自动生成构造函数存根
12     }
13 
14     public String getFirst() {
15         return first;
16     }
17 
18     public void setFirst(String first) {
19         this.first = first;
20     }
21 
22     public String getOperator() {
23         return operator;
24     }
25 
26     public void setOperator(String operator) {
27         this.operator = operator;
28     }
29 
30     public double getResult() {
31         return result;
32     }
33 
34     public void setResult(double result) {
35         this.result = result;
36     }
37 
38     public String getSecond() {
39         return second;
40     }
41 
42     public void setSecond(String second) {
43         this.second = second;
44     }
45     
46     /*
47      * 根据不同操作符进行计算
48      */
49     public void calculate() {
50         
51         try {
52             double one = Double.parseDouble(first);
53             double two = Double.parseDouble(second);
54             
55             if (operator.equals("+")) {
56                 result = one + two;
57             } else if (operator.equals("-")) {
58                 result = one - two;
59             } else if (operator.equals("*")) {
60                 result = one * two;
61             } else if (operator.equals("/")) {
62                 result = one / two;
63             }
64         } catch (NumberFormatException e) {
65             // TODO 自动生成 catch 块
66             e.printStackTrace();
67         } 
68         
69         
70     }
71     
72 }

sample9.jsp
 1 <%@ page language="java" contentType="text/html; charset=GB2312"%>
 2 <jsp:useBean id="calculator" scope="request" class="com.coderdream.bean.SampleBean5" >
 3     <jsp:setProperty name="calculator" property="*"/>
 4 </jsp:useBean>
 5 <html>
 6   <head>
 7     <title>简单计数器</title>
 8     <meta http-equiv="content-type" content="text/html; charset=GB2312">
 9   </head>
10   <body>
11       <align="center">
12           <b>简单的计算器</b>
13           <hr>
14           计算结果:
15           <%
16               try {
17                   calculator.calculate();
18                   out.println(calculator.getFirst() 
19                                           + calculator.getOperator() 
20                                           + calculator.getSecond()
21                                           + " = " +
22                                           + calculator.getResult());
23               } catch (Exception e) {
24                   System.out.println(e);
25               }
26 
27           %>
28       
29       </p>
30       <hr>
31       <form action="sample9.jsp" method="get">
32           <table>
33               <tr>
34                   <td align="right">第一个参数</td>
35                   <td><input type="text" name="first"></td>
36               </tr>
37               <tr>
38                   <td align="right">操作符</td>
39                   <td>
40                   
41                       <select name="operator">
42                           <option value="+">+</option>
43                           <option value="-">-</option>
44                           <option value="*">*</option>
45                           <option value="/">/</option>
46                       </select>
47                   </td>
48               </tr>
49               <tr>
50                   <td align="right">第二个参数</td>
51                   <td><input type="text" name="second"></td>
52               </tr>
53             <tr>
54                   <td align="center" colspan="2">
55                       <input type="submit" value="计算">
56                   </td>
57               </tr>
58           </table>
59       </form>
60   </body>
61 </html>

运行结果:

snap184.gif


《全文完》
posted on 2006-09-03 23:54 CoderDream 阅读(1895) 评论(4)  编辑  收藏 所属分类: JSP

FeedBack:
# re: JSP与JavaBean
2006-09-07 22:14 | P
请问这是哪里的视频?能告诉我哪里下载到这一系列视频吗?  回复  更多评论
  
# re: JSP与JavaBean
2006-09-08 19:24 | CoderDream
吉林大学的,verycd上面下载的!  回复  更多评论
  
# re: JSP与JavaBean
2007-10-24 09:15 | fancycloud
能不能给个verycd的连接啊,谢谢  回复  更多评论
  
# re: JSP与JavaBean

只有注册用户登录后才能发表评论。


网站导航:
 

<2006年9月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(9)

我参与的团队

随笔分类(245)

随笔档案(239)

文章分类(3)

文章档案(3)

收藏夹(576)

友情链接

搜索

  •  

积分与排名

  • 积分 - 429012
  • 排名 - 118

最新评论

阅读排行榜

评论排行榜