File : CounterService.java
package com.tw511.common; public class CounterService{ public void printCounter(int count){ System.out.println("Total session created : " + count); } }
File : counter.xml – Bean組態檔案
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="counterService" class="com.tw511.common.CounterService" /> </beans>
File : SessionCounterListener.java
package com.tw511.common; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; public class SessionCounterListener implements HttpSessionListener { private static int totalActiveSessions; public static int getTotalActiveSession(){ return totalActiveSessions; } @Override public void sessionCreated(HttpSessionEvent arg0) { totalActiveSessions++; System.out.println("sessionCreated - add one session into counter"); printCounter(arg0); } @Override public void sessionDestroyed(HttpSessionEvent arg0) { totalActiveSessions--; System.out.println("sessionDestroyed - deduct one session from counter"); printCounter(arg0); } private void printCounter(HttpSessionEvent sessionEvent){ HttpSession session = sessionEvent.getSession(); ApplicationContext ctx = WebApplicationContextUtils. getWebApplicationContext(session.getServletContext()); CounterService counterService = (CounterService) ctx.getBean("counterService"); counterService.printCounter(totalActiveSessions); } }
File : web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/Spring/counter.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class> com.tw511.common.SessionCounterListener </listener-class> </listener> <servlet> <servlet-name>Spring DI Servlet Listener</servlet-name> <servlet-class>com.tw511.common.App</servlet-class> </servlet> <servlet-mapping> <servlet-name>Spring DI Servlet Listener</servlet-name> <url-pattern>/Demo</url-pattern> </servlet-mapping> </web-app>
File : App.java
package com.tw511.common; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class App extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{ HttpSession session = request.getSession(); //sessionCreated() is executed session.setAttribute("url", "tw511.com"); session.invalidate(); //sessionDestroyed() is executed PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<body>"); out.println("<h1>Spring Dependency Injection into Servlet Listenner</h1>"); out.println("</body>"); out.println("</html>"); } }
啟動Tomcat,並存取 URL 「http://localhost:8080/7.2-SpringDemo/"
輸出結果
sessionCreated - add one session into counter Total session created : 1 sessionDestroyed - deduct one session from counter Total session created : 0