首先寫一個計數的類OnlineCounter
package accp.onlinecounter;
public class OnlineCounter {
private static long online = 0;
public static long getOnline() {
return online;
}
public static void raise() {
online++;
}
public static void reduce() {
online--;
}
}
之后寫一個實現HttpSessionEvent的類OnlineCounterListener
package accp.onlinecounter;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class OnlineCounterListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent hse) {
OnlineCounter.raise();
}
public void sessionDestroyed(HttpSessionEvent hse) {
OnlineCounter.reduce();
}
}
在web.xml中寫listener的注冊信息
<listener>
<listener-class>
accp.onlinecounter.OnlineCounterListener
</listener-class>
</listener>
前臺界面寫上
<body>
在線人數: <%=OnlineCounter.getOnline() %><br/>
<a href="adcourse.jsp">添加課程add course</a><br/>
<a href="adds.jsp">添加學生add stu</a><br/>
<a href="findallcourse.jsp">查詢課程信息 select course</a><br/>
<a href="findallstudent.jsp">查詢學生信息 select student</a><br/>
<a href="addstudentcourse.jsp">添加選課信息add student course</a><br/>
<a href="querystucourse.jsp">查詢選課信息query student course</a><br/>
</body>
注意引入包即可