日期:2014-05-16 浏览次数:20363 次
Counter.java
package cn.xd.model; public class Counter { private long counter; public long getCounter(){ return counter; } public void setCounter(long counter){ this.counter=counter+1; } }Counter.jsp
<body> <jsp:useBean id="counter" scope="application" class="cn.xd.model.Counter" /> <% if(session.isNew()){ long temp=counter.getCounter();
counter.setCounter(temp);
}
%>
you are the <jsp:getProperty name="counter" property="counter"/><br/>person.
</body>
现在来改正一下这段代码再看一下。
Counter.java
package cn.xd.model; public class Counter { private long counter; public void setCounter(){ counter++; } }Counter.jsp
<jsp:useBean id="counter" scope="application" class="cn.xd.model.Counter" /> <% if(session.isNew()){ counter.setCounter(); } %> you are the <jsp:getProperty name="counter" property="counter"/><br/>person. </body>
说明:我用的浏览器是火狐和Chrome,单打开一个窗口时不计数,原因是session生命周期也和cookie有关,必须重新打开浏览器才能计数。。
注意:
在web环境下调用Javabean一定要注意bean的作用范围,和特性。。