一、概述html
二、攔截器java
三、過濾器的生命週期web
四、初始化參數和過濾器鏈session
五、過濾器的使用場合jsp
六、監聽器post
web應用程序事件模型的一部分,當web應用中的某些狀態發生改變時,會產生相應的事件,監聽器能夠接收這些事件ui
而且在事件發生時作一些相關的處理。this
樣例:spa
package com.ljb.constants; public class Constants { public static int ONLINE_USER_COUNT = 0;// 在線用戶數 }
package com.ljb.listener; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; import com.ljb.constants.Constants; /** * Application Lifecycle Listener implementation class User * */ @WebListener public class User implements HttpSessionBindingListener { private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } /** * Default constructor. */ public User() { // TODO Auto-generated constructor stub } /** * 從session中刪除時調用 * @see HttpSessionBindingListener#valueUnbound(HttpSessionBindingEvent) */ public void valueUnbound(HttpSessionBindingEvent arg0) { Constants.ONLINE_USER_COUNT--; } /** * User對象存入session時自動調用 * @see HttpSessionBindingListener#valueBound(HttpSessionBindingEvent) */ public void valueBound(HttpSessionBindingEvent arg0) { Constants.ONLINE_USER_COUNT++; } }
enter.jspcode
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="doEnter.jsp" method="post"> 用戶名:<input type="text" name="username"/> <input type="submit" value="進入"/> </form> </body> </html>
doEnter.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.ljb.listener.User" %> <% String name = request.getParameter("username"); System.out.println(name); if (name == null || name.equals("")) { System.out.println("----------------1"); response.sendRedirect("enter.jsp"); } else { System.out.println("----------------2"); User user = new User(); user.setUsername(name); session.setAttribute("user", user);// 對象存入session會激發監聽器中valueBound方法的運行 response.sendRedirect("online.jsp"); } %>
online.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.ljb.listener.User,com.ljb.constants.Constants" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% User user = null; if (session.getAttribute("user") == null) { response.sendRedirect("enter.jsp"); } else { user = (User)session.getAttribute("user"); %> 歡迎你:<%=user.getUsername() %> 此時在線人數爲:<%=Constants.ONLINE_USER_COUNT %> <a href="doOut.jsp">離開</a> <%} %> </body> </html>
doOut.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% session.invalidate(); response.sendRedirect("enter.jsp"); %> </body> </html>
執行結果:
歡迎你:123 此時在線人數爲:2 離開
七、小結
八、初識MVC