本文共 7348 字,大约阅读时间需要 24 分钟。
servlet监听器的主要目的是给web应用增加事件处理机制,以便更好的监视和控制web应用的状态变化,从而在后台调用相应处理程序。
Request事件监听器
HttpSession事件监听器
ServletContext事件监听器
application是ServletContext的实例,由JSP容器默认创建。Servlet中调用getServletContext()方法得到ServletContext的实例。
全局对象即Application范围的对象,初始化阶段的变量值在web.xml中,经由<context-param>元素所设定的变量,它的范围也是Application的范围
用于监听Web应用启动和销毁的事件,监听器类需要实现ServletContextListener接口。
该接口的主要方法;
void contextInitialized(ServletContextEvent se):通知正在接受的对象,应用程序已经被加载及初始化
void contextDestroyed(ServletContextEvent se):通知正在接受的对象,应用程序已经被销毁
ServletContextEvent的主要方法:ServletContext getServletContext():取得当前的ServletContext对象
用于监听Web应用属性改变的事件,包括增加、删除、修改属性。监听器类需要实现ServletContextAttributeListener接口
ServletContextAttributeListener接口的主要方法:
void attributeAdded(ServletContextAttributeEvent se):若有对象加入Application的范围,通知正在收听的对象。
void attributeRemoved(ServletContextAttributeEvent se):若有对象从Application范围移除,通知正在收听的对象。
void attributeReplaced(ServletContextAttributeEvent se):若在Application的范围中,有对象取代另一个对象时,通知正在收听的对象
ServletContextAttributeEvent中的主要方法:
getName():返回属性名称
getValue()返回属性的值
主要方法:
sessionCreated(HttpSessionEvent se):session创建
sessionDestroyed(HttpSessionEvent se):session销毁
监听器监听Http会话的情况
监听HttpSession中属性的操作
该接口的主要方法:
void attributeAdded(HttpSessionBindingEvent se):监听Http会话中的属性添加
void attributeRemoved(HttpSessionBindingEvent se):监听Http会话中的属性移除
void attributeReplaced(HttpSessionBindingEvent se):监听Http会话中的属性更改操作
监听Http会话中对象的绑定信息
getSession():获取session对象
getName():返回session增加、删除、或替换的属性名称
getValue():返回session增加、删除、或替换的属性值
package listener;import java.util.Vector;public class UserList { private static Vector online = new Vector(); //添加在线人数 public static void addUser(String userName){ online.addElement(userName); } //移除在线人数 public static void removeUser(String userName){ online.removeElement(userName); } //获取在线用户数量 public static int getUserCount(){ return online.size(); } public static Vector getVector(){ return online; }}****Vector 类提供了实现可增长数组的功能,随着更多元素加入其中,数组变的更大。在删除一些元素之后,数组变小。
Vector 有三个构造函数,
public Vector(int initialCapacity,int capacityIncrement)
public Vector(int initialCapacity)
public Vector()
Vector 运行时创建一个初始的存储容量initialCapacity,存储容量是以capacityIncrement 变量定义的增量增长。初始的存储容量和capacityIncrement 可以在Vector 的构造函数中定义。第二个构造函数只创建初始存储容量。第三个构造函数既不指定初始的存储容量也不指定capacityIncrement。
Vector 类提供的访问方法支持类似数组运算和与Vector 大小相关的运算。类似数组的运算允许向量中增加,删除和插入元素。
****
package listener;import javax.servlet.http.HttpSessionAttributeListener;import javax.servlet.http.HttpSessionBindingEvent;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;public class OnlineListener implements HttpSessionListener,HttpSessionAttributeListener{ //监听Http会话中的属性添加 public void attributeAdded(HttpSessionBindingEvent se) { // TODO Auto-generated method stub UserList.addUser(String.valueOf(se.getValue()));//增加一个用户 System.out.println("session("+se.getSession().getId()+")增加属性"+se.getName()+",值为"+se.getValue()); } //监听Http会话中的属性移除 public void attributeRemoved(HttpSessionBindingEvent se) { // TODO Auto-generated method stub UserList.removeUser(String.valueOf(se.getValue())); System.out.println(se.getValue()+"属性已移除"); } //监听Http会话中的属性更改操作 public void attributeReplaced(HttpSessionBindingEvent se) { // TODO Auto-generated method stub String oldValue=String.valueOf(se.getValue());//旧的属性 String newValue=String.valueOf(se.getSession().getAttribute(se.getName()));//新的属性 UserList.removeUser(oldValue);//移除旧的属性 UserList.addUser(newValue);//增加新的属性 System.out.println(oldValue+"属性已更改为"+newValue); } public void sessionCreated(HttpSessionEvent se) { // TODO Auto-generated method stub System.out.println("会话已创建!"); } public void sessionDestroyed(HttpSessionEvent se) { // TODO Auto-generated method stub System.out.println("会话已销毁!"); }}
package listener;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class LoginServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub String userName=req.getParameter("userName");//获取前台传来的userName属性 HttpSession session = req.getSession(); session.setAttribute("userName", userName);//将属性保存到session会话中 resp.sendRedirect("index.jsp");//重定向到index.jsp页面 } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub doGet(req, resp); }}
package listener;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ExitServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub req.getSession().removeAttribute("userName");//从session中移除对象 resp.sendRedirect("login.jsp");//重定向到用户登录页面 } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub doGet(req, resp); }}
listener.OnlineListener LoginServlet listener.LoginServlet ExitServlet listener.ExitServlet LoginServlet /logindo ExitServlet /exitdo
<% //如果未登录,转向登录页面 if (session.getAttribute("userName") == null) { response.sendRedirect("login.jsp"); } Vector onlineUsers = UserList.getVector(); //获取存储在线用户名的vector对象 %>登录成功
欢迎你 <%=session.getAttribute("userName")%> 退出登录 当前在线人数: <%=UserList.getUserCount()%>人 在线用户名单 :