案例——基于JavaBean求任意两代数和
1 //Add.java 2 package beans; 3 4 public class Add 5 { 6 private int data1; 7 private int data2; 8 private int sum; 9 public Add(){} 10 public int getData1(){return data1;} 11 public void setData1(int data1){this.data1=data1;} 12 public int getData2(){return data2;} 13 public void setData2(int data2){this.data2=data2;} 14 public int getSum(){return data1+data2;} 15 public void setSum(int sum){this.sum=sum;} 16 }
1 //input.jsp 2 <%@ page language="java" pageEncoding="UTF-8"%> 3 <form action="show.jsp" method="post"> 4 <table> 5 <tr><td align="right">加数:</td> 6 <td><input name="data1"></td> 7 </tr> 8 <tr><td align="right">被加数:</td> 9 <td><input name="data2"></td> 10 </tr> 11 <tr><td></td> 12 <td><input type="submit" value="提交"></td> 13 </tr> 14 </table> 15 </form>
1 //show.jsp 2 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 3 <html> 4 <head> 5 <title>利用JavaBean+JSP求两数和</title> 6 </head> 7 8 <body> 9 <jsp:useBean id="c" class="beans.Add" scope="request"/> 10 <jsp:setProperty name="c" property="*"/> 11 <p>调用jsp:getProperty作标签显示结果值:<br> 12 <jsp:getProperty name="c" property="data1"/>+ 13 <jsp:getProperty name="c" property="data2"/>= 14 <jsp:getProperty name="c" property="sum"/> 15 </p> 16 </body> 17 </html>