首先写一个计数的类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>
注意引入包即可