jsp里面实现asp.net的Global文件内容。
1 import javax.servlet.ServletContext; 2 import javax.servlet.ServletContextEvent; 3 import javax.servlet.ServletContextListener; 4 import javax.servlet.http.HttpServlet; 5 import javax.servlet.http.HttpSession; 6 import javax.servlet.http.HttpSessionEvent; 7 import javax.servlet.http.HttpSessionListener; 8 9 10 public class Global extends HttpServlet implements ServletContextListener, 11 HttpSessionListener { 12 13 public void contextInitialized(ServletContextEvent arg0) { 14 // TODO Auto-generated method stub 15 System.out.println("网站启动"); 16 ServletContext application = arg0.getServletContext(); 17 application.setAttribute("count", 10); 18 } 19 20 public void contextDestroyed(ServletContextEvent arg0) { 21 // TODO Auto-generated method stub 22 System.out.println("网站停止"); 23 ServletContext application = arg0.getServletContext(); 24 application.setAttribute("count", 0); 25 } 26 27 public void sessionCreated(HttpSessionEvent arg0) { 28 // TODO Auto-generated method stub 29 System.out.println("创建一个session"); 30 HttpSession session = arg0.getSession(); 31 session.setAttribute("count", 10); 32 33 ServletContext application = session.getServletContext(); 34 Object obj = application.getAttribute("count"); 35 if(obj instanceof Integer){ 36 Integer appCount = (Integer)obj; 37 application.setAttribute("count", appCount); 38 } 39 } 40 41 public void sessionDestroyed(HttpSessionEvent arg0) { 42 // TODO Auto-generated method stub 43 System.out.println("销毁一个session"); 44 } 45 46 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 4 <listener> 5 <listener-class>Global</listener-class> 6 </listener> 7 </web-app>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。