大学jsp实验七--JavaBean在JSP中的应用

值bean的应用

(1)若有如下图所示的两个JSP页面,在第一个的页面的表单中填写相应内容,提交后再显示用户的提交信息。使用JavaBean的方式实现上述效果,请写出相应的代码。  

 

bean文件:Test2_1.java:

 1 package com.lgqchinese.webJSPTest.shiyan4;
 2 
 3 public class Test2_1 {
 4     String bookName = "";
 5     String bookNum = "";
 6     double bookPrice = 0;
 7     int bookPage = 0;
 8 
 9     public void setBookName(String bookName) {
10         try {
11             byte[] b = bookName.getBytes("Iso-8859-1");
12             bookName = new String(b,"utf-8");
13         } catch (Exception e) {
14 
15         }
16         this.bookName = bookName;
17     }
18 
19     public void setBookNum(String bookNum) {
20         this.bookNum = bookNum;
21     }
22 
23     public void setBookPrice(double bookPrice) {
24         this.bookPrice = bookPrice;
25     }
26 
27     public void setBookPage(int bookPage) {
28         this.bookPage = bookPage;
29     }
30 
31     public String getBookName() {
32         return bookName;
33     }
34 
35     public String getBookNum() {
36         return bookNum;
37     }
38 
39     public double getBookPrice() {
40         return bookPrice;
41     }
42 
43     public int getBookPage() {
44         return bookPage;
45     }
46 }

from.jsp:

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <jsp:useBean id="book" class="com.lgqchinese.webJSPTest.shiyan4.Test2_1" scope="request"/>
 3 <html>
 4 <head>
 5     <title>Title</title>
 6 </head>
 7 <body>
 8 <p>请输入书籍信息:</p>
 9 <form action="gain.jsp" method="post">
10     书名:<input type="text" name="bookName">
11     书号:<input type="text" name="bookNum">
12     价钱:<input type="text" name="bookPrice">
13     页数:<input type="text" name="bookPage">
14     <input type="submit" value="提交">
15     <input type="reset" value="重置">
16 </form>
17 </body>
18 </html>

gain.jsp:

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <jsp:useBean id="book" class="com.lgqchinese.webJSPTest.shiyan4.Test2_1" scope="request"/>
 3 <html>
 4 <head>
 5     <title>Title</title>
 6 </head>
 7 <body>
 8 <p>您输入的书籍信息如下:</p>
 9 <jsp:setProperty name="book" property="*"/>
10 书名:<jsp:getProperty name="book" property="bookName"/>
11 <br>
12 书号:<jsp:getProperty name="book" property="bookNum"/>
13 <br>
14 价钱:<jsp:getProperty name="book" property="bookPrice"/>
15 <br>
16 页数:<jsp:getProperty name="book" property="bookPage"/>
17 
18 </body>
19 </html>

(2)编写一个可以实现四则运算的Bean,实现如下图所示的效果,请写出相应代码。

bean文件:Test_2:

 1 package com.lgqchinese.webJSPTest.shiyan4;
 2 
 3 public class Test2_2 {
 4     double a = 0;
 5     double b = 0;
 6     double result = 0;
 7     String oper = "+";
 8 
 9     public void setA(double a) {
10         this.a = a;
11     }
12 
13     public void setB(double b) {
14         this.b = b;
15     }
16 
17     public void setOper(String o) {
18         oper = o.trim();
19     }
20 
21     public double getA() {
22         return a;
23     }
24 
25     public double getB() {
26         return b;
27     }
28 
29     public double getResult() {
30         return result;
31     }
32 
33     public String getOper() {
34         return oper;
35     }
36 
37     public double method() {
38         if (oper.equals("+")){
39             result = a+b;
40         }
41         else if (oper.equals("-")){
42             result = a-b;
43         }
44         else if (oper.equals("*")){
45             result = a*b;
46         }
47         else if (oper.equals("/")&&b!=0){
48             result = a/b;
49         }
50         return result;
51     }
52 }

Test_2.jsp:

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <jsp:useBean id="hello" class="com.lgqchinese.webJSPTest.shiyan4.Test2_2" scope="session"/>
 3 <html>
 4 <head>
 5     <title>四则运算</title>
 6 </head>
 7 <body>
 8 <jsp:setProperty name="hello" property="*"/>
 9 <form action="" method="post" name="form">
10     <input type="text" name="a">
11     <select name="oper">
12         <option value="+">+
13         <option value="-">-
14         <option value="*">*
15         <option value="/">/
16     </select>
17     <input type="text" name="b">
18     <br>
19     <input type="submit" value="提交">
20 </form>
21 
22 <jsp:getProperty name="hello" property="a"/>
23 <jsp:getProperty name="hello" property="oper"/>
24 <jsp:getProperty name="hello" property="b"/>
25 <jsp:getProperty name="hello" property="result"/>
26 </body>
27 </html>

 

posted @ 2018-05-18 15:16  龙谷情Sinoam  阅读(1932)  评论(0编辑  收藏  举报
Smiley face