使用java监听器统计在线人数
1. 监听器的简介
监听器就是一个实现特定接口的普通java程序,这个程序专门用于监听另一个java对象的方法调用或属性改变,当被监听对象发生上述事件后,监听器某个方法将立即被执行。
- 怎么去使用java监听器
ServletContextListener:用于监听WEB
应用启动和销毁的事件,监听器类需要实现javax.servlet.ServletContextListener 接口。
ServletContextAttributeListener:用于监听WEB应用属性改变的事件,包括:增加属性、删除属性、修改属性,监听器类需要实现javax.servlet.ServletContextAttributeListener接口。
HttpSessionListener:用于监听Session对象的创建和销毁,监听器类需要实现javax.servlet.http.HttpSessionListener接口或者javax.servlet.http.HttpSessionActivationListener接口,或者两个都实现。
HttpSessionActivationListener:用于监听Session对象的钝化/活化事件,监听器类需要实现javax.servlet.http.HttpSessionListener接口或者javax.servlet.http.HttpSessionActivationListener接口,或者两个都实现。
HttpSessionAttributeListener:用于监听Session对象属性
的改变事件,监听器类需要实现javax.servlet.http.HttpSes sionAttributeListener接口。
3. 我会以java监听器统计在线人数的实例来告诉大家
(1)先创建监听器包listener在新建OnlineListener类
package listener;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionBindingEvent;
@WebListener()
public class OnlineListener implements ServletContextListener,
HttpSessionListener, HttpSessionAttributeListener {
private static int count=0;
// Public constructor is required by servlet spec
public OnlineListener() {//构造函数
}
// -------------------------------------------------------
// ServletContextListener implementation
// -------------------------------------------------------
public void contextInitialized(ServletContextEvent sce) {//应用初始化完成
/* This method is called when the servlet context is
initialized(when the Web application is deployed).
You can initialize servlet context related data here.
*/
}
public void contextDestroyed(ServletContextEvent sce) {
/* This method is invoked when the Servlet Context
(the Web application) is undeployed or
Application Server shuts down.
*/
}
// -------------------------------------------------------
// HttpSessionListener implementation
// -------------------------------------------------------
public void sessionCreated(HttpSessionEvent se) {//创建
/* Session is created. */
count++;
ServletContext app = se.getSession().getServletContext();
app.setAttribute("count",count);
}
public void sessionDestroyed(HttpSessionEvent se) {//销毁
/* Session is destroyed. */
count--;
ServletContext app = se.getSession().getServletContext();
app.setAttribute("count",count);
}
}
3. 创建接口
(2)创建监听器servlet的接口
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "OnlineServlet",urlPatterns = "/online")
public class OnlineServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String op=request.getParameter("op");
String result="";
switch (op){
case "login":
//request.getSession()触发监听器的sessionCreated
ServletContext app=request.getSession().getServletContext();
int count= (int) app.getAttribute("count");
result=String.format("当前%d人在线",count);
break;
case "logout":
//销毁session,触发监听器的sessionDestroy
request.getSession().invalidate();//销毁session
result="您已经退出了!";
}
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
out.write(result);
out.close();
}
}
可以在浏览器上输入要访问的IP地址,会显示当前在线的人数,你也可以访问别人的IP地址
前提是别人是允许方法IP地址的