1、Servletjava
Servlet是基本的服務端程序,他來自接口Servlet,接口中有方法service。而Servlet的一個重要實現類,則是tomcat服務器的核心,那就是HttpServletweb
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的這幾個方法處理的(除非被過濾器攔截)tomcat
2、Listener服務器
Listener是監聽器,用於監聽Servlet,他是基於觀察者模式的,他的核心接口是ServletContextListener,繼承自EventListener。session
有如下方法:app
public void contextInitialized(ServletContextEvent sce)網站
public void contextDestroyed(ServletContextEvent sce)spa
其中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 |
分別介紹:
1、 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()
回傳屬性的值
2、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的情形。
4、ServletRequest監聽接口
1.ServletRequestListener接口
和ServletContextListener接口相似的,這裏由ServletContext改成ServletRequest
2.ServletRequestAttributeListener接口
和ServletContextListener接口相似的,這裏由ServletContext改成ServletRequest
有的listener可用於統計網站在線人數及訪問量。 以下:
服務器啓動時(實現ServletContextListener監聽器contextInitialized方法),讀取數據庫,並將其用一個計數變量保存在application範圍內
session建立時(實現HttpSessionListener監聽器sessionCreated方法),讀取計數變量加1並從新保存
服務器關閉時(實現ServletContextListener監聽器contextDestroyed方法),更新數據庫。
3、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。