标签:alt ota contex att index.jsp created class gevent url
1 import javax.servlet.ServletContext; 2 import javax.servlet.ServletContextEvent; 3 import javax.servlet.ServletContextListener; 4 import javax.servlet.annotation.WebListener; 5 import javax.servlet.http.HttpSessionAttributeListener; 6 import javax.servlet.http.HttpSessionEvent; 7 import javax.servlet.http.HttpSessionListener; 8 import javax.servlet.http.HttpSessionBindingEvent; 9 10 @WebListener() 11 public class CountListener implements HttpSessionListener { 12 private int count=0; 13 public void sessionCreated(HttpSessionEvent hse) { 14 count++; 15 ServletContext context = hse.getSession ().getServletContext (); 16 context.setAttribute ("count",new Integer (count)); 17 } 18 19 @Override 20 public void sessionDestroyed(HttpSessionEvent hse) { 21 count--; 22 ServletContext context = hse.getSession ().getServletContext (); 23 context.setAttribute ("count",new Integer (count)); 24 } 25 }
index.jsp
1 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 <html> 3 <head> 4 <title>$Title$</title> 5 </head> 6 <body> 7 <h3> 8 当前在线人数为:<%=application.getAttribute("count")%> 9 </h3> 10 <a href="<%=response.encodeURL ("logout.jsp")%>">退出系统</a> 11 </body> 12 </html>
logout.jsp
1 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 <html> 3 <head> 4 <title>Title</title> 5 </head> 6 <body> 7 <%session.invalidate();%> 8 <h3>您已退出系统</h3> 9 </body> 10 </html>
采用不同的浏览器访问,防止公用一个session对象
标签:alt ota contex att index.jsp created class gevent url
原文地址:http://www.cnblogs.com/gdwkong/p/7635054.html