JSP+JavaBean生成的计数器
(1)JavaBean代码
package com.bean;
public class Count {
private int time;
public Count(){
}
public int getTime() {
//每调用一次就加1
time++;
return time;
}
public void setTime(int time) {
this.time = time;
}
}
(2)JSP页面
<body>
<jsp:useBean id="ct" class="com.bean.Count" scope="application"/>
<center>
当前页面的访问次数:
<jsp:getProperty property="time" name="ct"/>
</center>
</body>
注意:这里的scope=“application”,即该JavaBean的生存范围是整个web应用的生命周期。
使用内置对象application生成的网页计数器
<body>
<h1>网页计数器</h1>
<%
if(application.getAttribute("time")==null)
application.setAttribute("time","1");
else
{
String times=application.getAttribute("time").toString();
int count=0;
count=Integer.parseInt(times);
count++;
application.setAttribute("time",Integer.toString(count));
}
%>
你是第<%=application.getAttribute("time") %>位访客
</body>