1 package javaBean; 2 3 public class pagecount { 4 5 private long count=0; 6 7 public long getcount() 8 { 9 return count; 10 } 11 public void setcount(long count) 12 { 13 this.count=count; 14 } 15 public void add() 16 { 17 count++; 18 } 19 20 }
1 <%@page import="javaBean.pagecount"%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <% 12 Object obj=application.getAttribute("pagecount"); 13 if(obj==null) 14 { 15 pagecount pc=new pagecount(); 16 application.setAttribute("pagecount", pc); 17 obj=pc; 18 } 19 pagecount pagec=(pagecount)obj; 20 pagec.add(); 21 out.print("计数器="+pagec.getcount()); 22 %> 23 </body> 24 </html>
练习版:
1 package javaBean; 2 3 public class JavaB3 { 4 5 private long count=0; 6 public long getcount() 7 { 8 return count; 9 } 10 public void setcount(long count) 11 { 12 this.count=count; 13 } 14 public void add() 15 { 16 count++; 17 } 18 19 }
1 <%@page import="javaBean.JavaB3"%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <% 12 Object obj=application.getAttribute("JavaB3"); 13 if(obj==null) 14 { 15 JavaB3 jb3=new JavaB3(); 16 application.setAttribute("JavaB3", jb3); 17 obj=jb3; 18 } 19 JavaB3 jb=(JavaB3)obj; 20 jb.add(); 21 out.print("<br>计数器:"+jb.getcount()); 22 %> 23 </body> 24 </html>