一、Servlet
Servlet是基本的服務端程序,他來自接口Servlet,接口中有方法service。而Servlet的一個重要實現類,則是tomcat服務器的核心,那就是HttpServlet
HttpServlet有方法:
public abstract class HttpServlet extends GenericServlet { private static final String METHOD_DELETE = "DELETE"; private static final String METHOD_HEAD = "HEAD"; private static final String METHOD_GET = "GET"; private static final String METHOD_OPTIONS = "OPTIONS"; private static final String METHOD_POST = "POST"; private static final String METHOD_PUT = "PUT"; private static final String METHOD_TRACE = "TRACE"; private static final String HEADER_IFMODSINCE = "If-Modified-Since"; private static final String HEADER_LASTMOD = "Last-Modified"; private static final String LSTRING_FILE = "javax.servlet.http.LocalStrings"; private static ResourceBundle lStrings = ResourceBundle.getBundle(LSTRING_FILE); protected void doGet(HttpServletRequest req, HttpServletResponse resp) protected void doHead(HttpServletRequest req, HttpServletResponse resp) protected void doPost(HttpServletRequest req, HttpServletResponse resp) protected void doPut(HttpServletRequest req, HttpServletResponse resp) protected void doDelete(HttpServletRequest req, HttpServletResponse resp) protected void doOptions(HttpServletRequest req, HttpServletResponse resp) protected void doTrace(HttpServletRequest req, HttpServletResponse resp)
}
幾個do方法是核心,所有的客戶端請求,最終都是通過Servlet的這幾個方法處理的(除非被過濾器攔截)
二、Listener
Listener是監聽器,用於監聽Servlet,他是基於觀察者模式的,他的核心接口是ServletContextListener,繼承自EventListener。
有以下方法:
public void contextInitialized(ServletContextEvent sce)
public void contextDestroyed(ServletContextEvent sce)
其中ServletContextEvent繼承自java.util.EventSource類,該類在構造方法中傳遞一個事件源進去,可以通過getServletContext方法來獲取ServletContext,ServletContext為事件源。(單純的一個簡單的Listener,僅僅用於啟動服務器時執行一些語句,則他的Source為一個ApplicationContextFacade,外觀模式,核心是整個web程序的上下文)
內置的監聽器可用於監聽屬性變化,Session創建等,一般來說是用於監聽Servlet的,Servlet監聽器用於監聽一些重要事件的發生,監聽器對象可以在事情發生前、發生后可以做一些必要的處理。
目前Servlet2.4和JSP2.0總共有8個監聽器接口和6個Event類,其中HttpSessionAttributeListener與HttpSessionBindingListener 皆使用HttpSessionBindingEvent;HttpSessionListener和 HttpSessionActivationListener則都使用HttpSessionEvent;其余Listener對應的Event如下所示:
Listener接口 |
Event類 |
ServletContextListener |
ServletContextEvent |
ServletContextAttributeListener |
ServletContextAttributeEvent |
HttpSessionListener |
HttpSessionEvent |
HttpSessionActivationListener |
|
HttpSessionAttributeListener |
HttpSessionBindingEvent |
HttpSessionBindingListener |
|
ServletRequestListener |
ServletRequestEvent |
ServletRequestAttributeListener |
ServletRequestAttributeEvent |
分別介紹:
一、 ServletContext相關監聽接口
補充知識:
通過ServletContext 的實例可以存取應用程序的全局對象以及初始化階段的變量。
在JSP文件中,application 是 ServletContext 的實例,由JSP容器默認創建。Servlet 中調用 getServletContext()方法得到 ServletContext 的實例。
注意:
全局對象即Application范圍對象,初始化階段的變量指在web.xml中,經由<context-param>元素所設定的變量,它的范圍也是Application范圍,例如:
<context-param>
<param-name>Name</param-name>
<param-value>browser</param-value>
</context-param>
當容器啟動時,會建立一個Application范圍的對象,若要在JSP網頁中取得此變量時:
String name = (String)application.getInitParameter("Name");
或者使用EL時:
${initPara.name}
若是在Servlet中,取得Name的值方法:
String name = (String)ServletContext.getInitParameter("Name");
1.ServletContextListener:
用於監聽WEB 應用啟動和銷毀的事件,監聽器類需要實現javax.servlet.ServletContextListener 接口。
ServletContextListener 是 ServletContext 的監聽者,如果 ServletContext 發生變化,如服務器啟動時 ServletContext 被創建,服務器關閉時 ServletContext 將要被銷毀。
ServletContextListener接口的方法:
void contextInitialized(ServletContextEvent sce)
通知正在接受的對象,應用程序已經被加載及初始化。
void contextDestroyed(ServletContextEvent sce)
通知正在接受的對象,應用程序已經被載出。
ServletContextEvent中的方法:
ServletContext getServletContext()
取得ServletContext對象
2.ServletContextAttributeListener:用於監聽WEB應用屬性改變的事件,包括:增加屬性、刪除屬性、修改屬性,監聽器類需要實現javax.servlet.ServletContextAttributeListener接口。
ServletContextAttributeListener接口方法:
void attributeAdded(ServletContextAttributeEvent scab)
若有對象加入Application的范圍,通知正在收聽的對象
void attributeRemoved(ServletContextAttributeEvent scab)
若有對象從Application的范圍移除,通知正在收聽的對象
void attributeReplaced(ServletContextAttributeEvent scab)
若在Application的范圍中,有對象取代另一個對象時,通知正在收聽的對象
ServletContextAttributeEvent中的方法:
java.lang.String getName()
回傳屬性的名稱
java.lang.Object getValue()
回傳屬性的值
二、HttpSession相關監聽接口
1.HttpSessionBindingListener接口
注意:HttpSessionBindingListener接口是唯一不需要再web.xml中設定的Listener
當我們的類實現了HttpSessionBindingListener接口后,只要對象加入 Session范圍(即調用HttpSession對象的setAttribute方法的時候)或從Session范圍中移出(即調用HttpSession對象的 removeAttribute方法的時候或Session Time out的時候)時,容器分別會自動調用下列兩個方法:
void valueBound(HttpSessionBindingEvent event)
void valueUnbound(HttpSessionBindingEvent event)
思考:如何實現記錄網站的客戶登錄日志, 統計在線人數?
2.HttpSessionAttributeListener接口
HttpSessionAttributeListener監聽HttpSession中的屬性的操作。
當在Session增加一個屬性時,激發attributeAdded(HttpSessionBindingEvent se) 方法;當在Session刪除一個屬性時,激發attributeRemoved(HttpSessionBindingEvent se)方法;當在Session屬性被重新設置時,激發attributeReplaced(HttpSessionBindingEvent se) 方法。這和ServletContextAttributeListener比較類似。
3.HttpSessionListener接口
HttpSessionListener監聽 HttpSession的操作。當創建一個Session時,激發session Created(HttpSessionEvent se)方法;當銷毀一個Session時,激發sessionDestroyed (HttpSessionEvent se)方法。
4.HttpSessionActivationListener接口
主要用於同一個Session轉移至不同的JVM的情形。
四、ServletRequest監聽接口
1.ServletRequestListener接口
和ServletContextListener接口類似的,這里由ServletContext改為ServletRequest
2.ServletRequestAttributeListener接口
和ServletContextListener接口類似的,這里由ServletContext改為ServletRequest
有的listener可用於統計網站在線人數及訪問量。 如下:
服務器啟動時(實現ServletContextListener監聽器contextInitialized方法),讀取數據庫,並將其用一個計數變量保存在application范圍內
session創建時(實現HttpSessionListener監聽器sessionCreated方法),讀取計數變量加1並重新保存
服務器關閉時(實現ServletContextListener監聽器contextDestroyed方法),更新數據庫。
三、Filter
Filter是過濾器,用於過濾到servlet的request,它使用戶可以改變一個 request和修改一個response. Filter 不是一個servlet,它不能產生一個response,它能夠在一個request到達servlet之前預處理request,也可以在離開 servlet時處理response.換種說法,filter其實是一個”servlet chaining”(servlet 鏈).
一個Filter包括:
1)、在servlet被調用之前截獲;
2)、在servlet被調用之前檢查servlet request;
3)、根據需要修改request頭和request數據;
4)、根據需要修改response頭和response數據;
5)、在servlet被調用之后截獲.
服務器每次只調用setFilterConfig方法一次准備filter 的處理;調用doFilter方法多次以處理不同的請求.FilterConfig接口有方法可以找到filter名字及初始化參數信息.服務器可以設置 FilterConfig為空來指明filter已經終結。
每一個filter從doFilter()方法中得到當前的request及response.在這個方法里,可以進行任何的針對request及 response的操作.(包括收集數據,包裝數據等).filter調用chain.doFilter()方法把控制權交給下一個filter.一個 filter在doFilter()方法中結束.如果一個filter想停止request處理而獲得對response的完全的控制,那它可以不調用下 一個filter。
這三者中,Servlet和Filter是可以配置mapping的,即針對哪些地址的請求使用這些Servlet或者Filter,而Listener則是根據實現接口來判斷什么情況下調用這個Listener的,基本的Listener僅僅在啟動時執行一些任務。如果一個請求,同時調用到這三個,則執行順序是:Listener,Filter,Servlet。
http://blog.csdn.net/agileclipse/article/details/9014683