JSP-JSP监听器

一、//在web.xml中配置listener


 cn.news.lisener.UserLisener
 

二、在com.wu.listener包下写一个UserListener类

HttpSessionBindingListener接口

valueBound(HttpSessionBindingEvent event)

当对象被添加到session时,由容器调用该方法来通知对象,无返回值

valueUnbound(HttpSessionBindingEvent event)

当对象从session中清除时,由容器调用该方法通知对象,无返回值

public class UserListener implements HttpSessionBindingListener {
//session生效
	public void valueBound(HttpSessionBindingEvent arg0) {
		Statistics.USER_ONLINE_STATISTICS++;
		
	}
	public void valueUnbound(HttpSessionBindingEvent arg0) {
		Statistics.USER_ONLINE_STATISTICS--;
	}
}	

//在UserServlet


UserListener ul = new UserListener();
		if (user != null) {
			// 如果正确,保存用户的状态信息
			if (user.getPwd().equals(upwd)) {
				System.out.println("登录成功");
				HttpSession session = request.getSession();
				session.setAttribute("loginuser", user.getUname());
				session.setAttribute("isadmin", user.getIsadmin());
				session.setAttribute("ul", ul);
				response.sendRedirect(request.getContextPath() + "/index.jsp");

JSP-JSP监听器_第1张图片

你可能感兴趣的:(Servlet)