监听器
1.什么是监听器
web监听器是Servlet中一种特殊的类,能帮助开发者监听Web中的特定事件,比如ServletContext,HttpSession,ServletRequest的创建和销毁;变量的创建、销毁和修改等,可以在某些动作前后增加处理,实现监控。例如可以用来统计在线人数等。
2.监听器的简单实现
1.Listener01.java
package com.xxxx.listener; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; /** * 实现接口:以生命周期为例子 */ @WebListener public class Listener01 implements HttpSessionListener { //当Session对象被创建的时候就会执行 @Override public void sessionCreated(HttpSessionEvent se) { System.out.println("session对象被创建"); } //当Session对象被销毁的时候就会被执行 @Override public void sessionDestroyed(HttpSessionEvent se) { System.out.println("session 对象被销毁"); } }
2.Servlet01.java
package com.xxxx.servlet; 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 javax.servlet.http.HttpSession; import java.io.IOException; @WebServlet("/s01") public class Servlet01 extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("Servlet01...."); //创建Session对象 HttpSession session = request.getSession(); } }
3.Servlet02.java
package com.xxxx.servlet; 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; @WebServlet("/s02") public class Servlet02 extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("Servlet02...."); //销毁Session对象 request.getSession().invalidate(); } }
3.案例
查看在线人数
存在的问题,不同浏览器中,看到的在线人数不一样,就算更新也还是不会改变,原因时session对象只在当前浏览器存活。
修改后:点击更新,在线人数也会实现更新了。
1.OnlineListener.java
package com.xxxx.listener; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; /** * 在线人数统计 * 当有新的Session对象创建时,人数+1。 * 当有Session对象销毁时,人数-1. */ @WebListener public class OnlineListener implements HttpSessionListener { //定义在线人数 private Integer onlineNumber = 0; @Override public void sessionCreated(HttpSessionEvent se) { //人数+1 onlineNumber++; //用Session不好,因为只在当前浏览器存活 //修改前se.getSession().setAttribute("onlineNumber",onlineNumber); //将人数设置到ServletContext作用域中 se.getSession().getServletContext().setAttribute("onlineNumber",onlineNumber); } @Override public void sessionDestroyed(HttpSessionEvent se) { //人数-1 onlineNumber--; //修改前:se.getSession().setAttribute("onlineNumber",onlineNumber); se.getSession().getServletContext().setAttribute("onlineNumber",onlineNumber); } }
2.OnlineServlet.java
package com.xxxx.servlet; 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 javax.servlet.http.HttpSession; import java.io.IOException; @WebServlet("/online") public class OnlineServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //创建Session对象 HttpSession session = req.getSession(); //修改前Integer onlineNumber = (Integer) session.getAttribute("onlineNumber"); Integer onlineNumber = (Integer) session.getServletContext().getAttribute("onlineNumber"); //设置响应类型及编码 resp.setContentType("text/html;charset=UTF-8"); resp.getWriter().write("<h2>当前人数:"+onlineNumber+"</h2>"); } }
销毁session对象:退出操作
1.OnlineListener.java
package com.xxxx.listener; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; /** * 在线人数统计 * 当有新的Session对象创建时,人数+1。 * 当有Session对象销毁时,人数-1. */ @WebListener public class OnlineListener implements HttpSessionListener { //定义在线人数 private Integer onlineNumber = 0; @Override public void sessionCreated(HttpSessionEvent se) { //人数+1 onlineNumber++; //用Session不好,因为只在当前浏览器存活 //修改前se.getSession().setAttribute("onlineNumber",onlineNumber); //将人数设置到ServletContext作用域中 se.getSession().getServletContext().setAttribute("onlineNumber",onlineNumber); } @Override public void sessionDestroyed(HttpSessionEvent se) { //人数-1 onlineNumber--; //修改前:se.getSession().setAttribute("onlineNumber",onlineNumber); se.getSession().getServletContext().setAttribute("onlineNumber",onlineNumber); } }
2.OnlineServlet.java
package com.xxxx.servlet; 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 javax.servlet.http.HttpSession; import java.io.IOException; @WebServlet("/online") public class OnlineServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //创建Session对象 HttpSession session = req.getSession(); //获取key String key = req.getParameter("key"); //判断key参数是否为空,如果不为空,则表示退出操作 if(key != null&&"logout".equals(key)){ //销毁Session对象 session.invalidate(); return ; } /*登录操作*/ //修改前Integer onlineNumber = (Integer) session.getAttribute("onlineNumber"); Integer onlineNumber = (Integer) session.getServletContext().getAttribute("onlineNumber"); //设置响应类型及编码 resp.setContentType("text/html;charset=UTF-8"); resp.getWriter().write("<h2>当前人数:"+onlineNumber+"</h2> <a href='online?key=logout'>退出</a>"); } }
标签:http,学习,监听器,import,onlineNumber,servlet,javax,se From: https://www.cnblogs.com/hmy22466/p/16845856.html