案例——网页计数器JavaBean的设计与使用
1 //Count.java 2 package beans; 3 4 public class Count { 5 private int count; 6 public Count(){count=0;} 7 public int getCount(){ 8 count++; 9 return count; 10 } 11 public void setCount(int count){this.count=count;} 12 }
//count1.jsp <%@ page language="java" pageEncoding="UTF-8"%> <html> <title>网页访问数量</title> </head> <body> <jsp:useBean id="count" scope="application" class="beans.Count"/> 这次访问的是第1个页面:count1.jsp!<br> 两页面共被访问次数: <jsp:getProperty name="count" property="count"/> </body> </html>
//count2.jsp <%@ page language="java" pageEncoding="UTF-8"%> <html> <title>网页访问数量</title> </head> <body> <jsp:useBean id="count" scope="application" class="beans.Count"/> 这次访问的是第2个页面:count2.jsp!<br> 两页面共被访问次数: <jsp:getProperty name="count" property="count"/> </body> </html>