一胀蛮、Listener監(jiān)聽(tīng)器
1并淋、概述
Listener是JavaEE的規(guī)范(接口)
作用:監(jiān)聽(tīng)某種事物的變化,通過(guò)回調(diào)函數(shù)宝当,反饋給客戶(程序)去做一些響應(yīng)的處理审胚。
2匈勋、使用
ServletContextListener監(jiān)聽(tīng)器
它監(jiān)聽(tīng)ServletContext對(duì)象的創(chuàng)建和銷(xiāo)毀。當(dāng)創(chuàng)建和銷(xiāo)毀時(shí)調(diào)用方法contextInitialized
和contextDestroyed
使用步驟:
1膳叨、編寫(xiě)實(shí)現(xiàn)類(lèi)洽洁,實(shí)現(xiàn)方法
2、web.xml配置Listener
<listener>
<listener-class>xxx</listener-class>
</listener>
二菲嘴、Filter過(guò)濾器
1饿自、概述
Listener是JavaEE的規(guī)范(接口)
作用:攔截請(qǐng)求汰翠,過(guò)濾響應(yīng)
攔截請(qǐng)求場(chǎng)景:
- 權(quán)限檢查
- 日記操作
- 事務(wù)管理
2、使用
使用步驟:
1昭雌、編寫(xiě)實(shí)現(xiàn)類(lèi)實(shí)現(xiàn)接口Filter复唤,實(shí)現(xiàn)doFilter方法
2、web.xml配置filter和filter-mapping
<filter>
<filter-name>filterName</filter-name>
<filter-class>xxx</filter-class>
</filter>
<filter-mapping>
<filter-name>filterName</filter-name>
<url-pattern>/</url-pattern>
</>
3烛卧、Filter生命周期
1佛纫、構(gòu)造器方法
2、init初始化方法
1/2步总放,在web啟動(dòng)的時(shí)候執(zhí)行(Filter已經(jīng)創(chuàng)建)
3呈宇、doFilter過(guò)濾方法
每次攔截請(qǐng)求,執(zhí)行一次
4局雄、destroy銷(xiāo)毀方法
停止web甥啄,執(zhí)行(銷(xiāo)毀Filter對(duì)象)
4、FilterConfig類(lèi)
Filter過(guò)濾器的配置文件類(lèi)
每次創(chuàng)建Filter的時(shí)候哎榴,也會(huì)同時(shí)創(chuàng)建一個(gè)FilterConfig類(lèi)型豁,這里包含了Filter配置文件的配置信息
FilterConfig類(lèi)的作用是獲取filter過(guò)濾器的配置內(nèi)容
1、獲取Filter的名稱(chēng)filter-name的內(nèi)容:.getFilterName()
2尚蝌、獲取在Filter中配置的init-param初始化參數(shù).getInitParameter("")
3、獲取ServletContext對(duì)象:.getServletContext()
5充尉、FilterChain類(lèi)
6飘言、Filter的攔截路徑
- 精確匹配
/xxx.html
- 目錄匹配
/*
- 后綴名匹配
*.html
(不能以/開(kāi)頭)
三、Servlet
1驼侠、概述
規(guī)范(接口)
是運(yùn)行在服務(wù)器上的一個(gè)java小程序姿鸿。可以接收客戶端發(fā)送過(guò)來(lái)的請(qǐng)求倒源,并響應(yīng)數(shù)據(jù)給客戶端
2苛预、實(shí)現(xiàn)
1、編寫(xiě)實(shí)現(xiàn)Servlet接口
2笋熬、實(shí)現(xiàn)service方法热某,處理請(qǐng)求,響應(yīng)數(shù)據(jù)(service方法專(zhuān)門(mén)用來(lái)處理請(qǐng)求和響應(yīng)的)
3胳螟、到web.xml中配置servlet程序的訪問(wèn)地址
<servlet>
<servlet-name>ServletName</servlet-name>
<servlet-class>xxx</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletName</servlet-name>
<url-pattern>/xxx</url-pattern>
</servlet-mapping>
3昔馋、url訪問(wèn)過(guò)程
4、Servlet的生命周期
1糖耸、servlet構(gòu)造器方法
2秘遏、init初始化方法
- 1/2在第一次訪問(wèn),調(diào)用
3嘉竟、service方法
- 每次訪問(wèn)調(diào)用一次
4邦危、destroy銷(xiāo)毀方法
- 停止web調(diào)用
繼承HttpServlet
重寫(xiě)doGet(doPost)方法
5洋侨、ServletConfig
Servlet程序和ServletConfig對(duì)象都是由Tomcat負(fù)責(zé)創(chuàng)建,我們負(fù)責(zé)使用倦蚪。
Servlet程序默認(rèn)是第一次訪問(wèn)的時(shí)候創(chuàng)建希坚,ServletConfig是每個(gè)Servlet程序創(chuàng)建時(shí),就創(chuàng)建一個(gè)對(duì)應(yīng)的ServletConfig對(duì)象审丘。
作用:
- 可以獲取Servlet程序的別名servlet-name的值
.getServletName()
- 獲取初始化值init-param
.getInitParameter("")
- 獲取ServletContext對(duì)象
.getServletContext()
獲取Config:getServletConfig()
初始化Config:在初始方法中保存config
6吏够、ServletContext
接口,表示Servlet上下文對(duì)象
一個(gè)web工程滩报,只有一個(gè)ServletCOntext對(duì)象實(shí)例
是一個(gè)域?qū)ο?br>
ServletContext在Web啟動(dòng)創(chuàng)建锅知,停止銷(xiāo)毀
作用:
- 獲取web.xml中配置的上下文參數(shù)context-param
.getInitParameter("");
<context-param>
<param-name>xxx</param-name>
<param-value>xxx</param-value>
</context-param>
- 獲取當(dāng)前的工程路徑,格式:/工程路徑
.getContextPath()
- 獲取工程部署后在服務(wù)器硬盤(pán)上的絕對(duì)路徑
.getRealPath("/")
- 像Map一樣存取數(shù)據(jù)
.setAttribute("","");
.getAttribute("");