java Web监听器导图详解
監(jiān)聽(tīng)器是JAVA Web開(kāi)發(fā)中很重要的內(nèi)容,其中涉及到的知識(shí),可以參考下面導(dǎo)圖:
Web監(jiān)聽(tīng)器
1 什么是web監(jiān)聽(tīng)器?
web監(jiān)聽(tīng)器是一種Servlet中的特殊的類,它們能幫助開(kāi)發(fā)者監(jiān)聽(tīng)web中的特定事件,比如ServletContext,HttpSession,ServletRequest的創(chuàng)建和銷毀;變量的創(chuàng)建、銷毀和修改等。可以在某些動(dòng)作前后增加處理,實(shí)現(xiàn)監(jiān)控。
2 監(jiān)聽(tīng)器常用的用途
通常使用Web監(jiān)聽(tīng)器做以下的內(nèi)容:
統(tǒng)計(jì)在線人數(shù),利用HttpSessionLisener
加載初始化信息:利用ServletContextListener
統(tǒng)計(jì)網(wǎng)站訪問(wèn)量
實(shí)現(xiàn)訪問(wèn)監(jiān)控
3 接下里看看一個(gè)監(jiān)聽(tīng)器的創(chuàng)建以及執(zhí)行過(guò)程
? 首先需要?jiǎng)?chuàng)建一個(gè)監(jiān)聽(tīng)器,實(shí)現(xiàn)某種接口,例如我想實(shí)現(xiàn)一個(gè)對(duì)在線人數(shù)的監(jiān)控,可以創(chuàng)建如下的監(jiān)聽(tīng)器:
public class MyListener implements HttpSessionListener{private int userNumber = 0;public void sessionCreated(HttpSessionEvent arg0) {userNumber++;arg0.getSession().setAttribute("userNumber", userNumber);}public void sessionDestroyed(HttpSessionEvent arg0) {userNumber--;arg0.getSession().setAttribute("userNumber", userNumber);} }然后在web.xml中配置該監(jiān)聽(tīng)器,在web-app中添加:
<listener><listener-class>com.test.MyListener</listener-class></listener>在JSP中添加訪問(wèn)人數(shù):
<body>在線人數(shù):<%=session.getAttribute("userNumber") %><br/> </body>當(dāng)我使用我的瀏覽器訪問(wèn)時(shí),執(zhí)行結(jié)果如下:
當(dāng)打開(kāi)另一個(gè)瀏覽器訪問(wèn)時(shí):
由于打開(kāi)另一個(gè)瀏覽器訪問(wèn),相當(dāng)于另一個(gè)會(huì)話,因此在線人數(shù)會(huì)增加。
對(duì)于3.0版本的Servlet來(lái)說(shuō),還支持使用注解的方式進(jìn)行配置。
那么接下來(lái)看看都有哪些監(jiān)聽(tīng)器以及方法吧!
監(jiān)聽(tīng)器的分類
1 按照監(jiān)聽(tīng)的對(duì)象劃分:
按照監(jiān)聽(tīng)對(duì)象的不同可以劃分為三種:
ServletContext監(jiān)控:對(duì)應(yīng)監(jiān)控application內(nèi)置對(duì)象的創(chuàng)建和銷毀。
當(dāng)web容器開(kāi)啟時(shí),執(zhí)行contextInitialized方法;當(dāng)容器關(guān)閉或重啟時(shí),執(zhí)行contextDestroyed方法。
實(shí)現(xiàn)方式:直接實(shí)現(xiàn)ServletContextListener接口:
public class MyServletContextListener implements ServletContextListener{public void contextDestroyed(ServletContextEvent sce) {}public void contextInitialized(ServletContextEvent sce) {} }HttpSession監(jiān)控:對(duì)應(yīng)監(jiān)控session內(nèi)置對(duì)象的創(chuàng)建和銷毀。
當(dāng)打開(kāi)一個(gè)新的頁(yè)面時(shí),開(kāi)啟一個(gè)session會(huì)話,執(zhí)行sessionCreated方法;當(dāng)頁(yè)面關(guān)閉session過(guò)期時(shí),或者容器關(guān)閉銷毀時(shí),執(zhí)行sessionDestroyed方法。
實(shí)現(xiàn)方式:直接實(shí)現(xiàn)HttpSessionListener接口:
public class MyHttpSessionListener implements HttpSessionListener{public void sessionCreated(HttpSessionEvent arg0) {}public void sessionDestroyed(HttpSessionEvent arg0) {} }ServletRequest監(jiān)控:對(duì)應(yīng)監(jiān)控request內(nèi)置對(duì)象的創(chuàng)建和銷毀。
當(dāng)訪問(wèn)某個(gè)頁(yè)面時(shí),出發(fā)一個(gè)request請(qǐng)求,執(zhí)行requestInitialized方法;當(dāng)頁(yè)面關(guān)閉時(shí),執(zhí)行requestDestroyed方法。
實(shí)現(xiàn)方式,直接實(shí)現(xiàn)ServletRequestListener接口:
public class MyServletRequestListener implements ServletRequestListener{public void requestDestroyed(ServletRequestEvent arg0) {}public void requestInitialized(ServletRequestEvent arg0) {} }?
2 按照監(jiān)聽(tīng)事件劃分:
2.1 監(jiān)聽(tīng)事件自身的創(chuàng)建和銷毀:同上面的按對(duì)象劃分。
2.2 監(jiān)聽(tīng)屬性的新增、刪除和修改:
監(jiān)聽(tīng)屬性的新增、刪除和修改也是劃分成三種,分別針對(duì)于ServletContext、HttpSession、ServletRequest對(duì)象:
ServletContext,實(shí)現(xiàn)ServletContextAttributeListener接口:
通過(guò)調(diào)用ServletContextAttribtueEvent的getName方法可以得到屬性的名稱。
public class MyServletContextAttrListener implements ServletContextAttributeListener{public void attributeAdded(ServletContextAttributeEvent hsbe) {System.out.println("In servletContext added :name = "+hsbe.getName());}public void attributeRemoved(ServletContextAttributeEvent hsbe) {System.out.println("In servletContext removed :name = "+hsbe.getName());}public void attributeReplaced(ServletContextAttributeEvent hsbe) {System.out.println("In servletContext replaced :name = "+hsbe.getName());}}HttpSession,實(shí)現(xiàn)HttpSessionAttributeListener接口:
public class MyHttpSessionAttrListener implements HttpSessionAttributeListener{public void attributeAdded(HttpSessionBindingEvent hsbe) {System.out.println("In httpsession added:name = "+hsbe.getName());}public void attributeRemoved(HttpSessionBindingEvent hsbe) {System.out.println("In httpsession removed:name = "+hsbe.getName());}public void attributeReplaced(HttpSessionBindingEvent hsbe) {System.out.println("In httpsession replaced:name = "+hsbe.getName());}}ServletRequest,實(shí)現(xiàn)ServletRequestAttributeListener接口:
public class MyServletRequestAttrListener implements ServletRequestAttributeListener{public void attributeAdded(ServletRequestAttributeEvent hsbe) {System.out.println("In servletrequest added :name = "+hsbe.getName());}public void attributeRemoved(ServletRequestAttributeEvent hsbe) {System.out.println("In servletrequest removed :name = "+hsbe.getName());}public void attributeReplaced(ServletRequestAttributeEvent hsbe) {System.out.println("In servletrequest replaced :name = "+hsbe.getName());}}2.3 監(jiān)聽(tīng)對(duì)象的狀態(tài):
針對(duì)某些POJO類,可以通過(guò)實(shí)現(xiàn)HttpSessionBindingListener接口,監(jiān)聽(tīng)POJO類對(duì)象的事件。例如:
public class User implements HttpSessionBindingListener,Serializable{private String username;private String password;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public void valueBound(HttpSessionBindingEvent hsbe) {System.out.println("valueBound name: "+hsbe.getName());}public void valueUnbound(HttpSessionBindingEvent hsbe) {System.out.println("valueUnbound name: "+hsbe.getName());}}Session數(shù)據(jù)的鈍化與活化:
由于session中保存大量訪問(wèn)網(wǎng)站相關(guān)的重要信息,因此過(guò)多的session數(shù)據(jù)就會(huì)服務(wù)器性能的下降,占用過(guò)多的內(nèi)存。因此類似數(shù)據(jù)庫(kù)對(duì)象的持久化,web容器也會(huì)把不常使用的session數(shù)據(jù)持久化到本地文件或者數(shù)據(jù)中。這些都是有web容器自己完成,不需要用戶設(shè)定。
不用的session數(shù)據(jù)序列化到本地文件中的過(guò)程,就是鈍化;
當(dāng)再次訪問(wèn)需要到該session的內(nèi)容時(shí),就會(huì)讀取本地文件,再次放入內(nèi)存中,這個(gè)過(guò)程就是活化。
類似的,只要實(shí)現(xiàn)HttpSeesionActivationListener接口就是實(shí)現(xiàn)鈍化與活化事件的監(jiān)聽(tīng):
public class User implements HttpSessionBindingListener, HttpSessionActivationListener,Serializable{private String username;private String password;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public void valueBound(HttpSessionBindingEvent hsbe) {System.out.println("valueBound name: "+hsbe.getName());}public void valueUnbound(HttpSessionBindingEvent hsbe) {System.out.println("valueUnbound name: "+hsbe.getName());}public void sessionDidActivate(HttpSessionEvent hsbe) {System.out.println("sessionDidActivate name: "+hsbe.getSource());}public void sessionWillPassivate(HttpSessionEvent hsbe) {System.out.println("sessionWillPassivate name: "+hsbe.getSource());}}Servlet版本與Tomcat版本
首先看一下Tomcat官網(wǎng)給出的匹配:
如果版本不匹配,那么tomcat是不能發(fā)布該工程的,首先看一下版本不匹配時(shí),會(huì)發(fā)生什么!
我試圖創(chuàng)建一個(gè)web工程,并且選取了Servlet3.0版本:
然后我想要在tomcat6中發(fā)布,可以看到報(bào)錯(cuò)了!
JDK版本不對(duì)....這是在平時(shí)開(kāi)發(fā)如果對(duì)Servlet不熟悉的web新手,常犯的錯(cuò)誤。
解決方法:
1 在創(chuàng)建時(shí),直接發(fā)布到Tomcat容器中,此時(shí)Servlet僅僅會(huì)列出Tomcat支持的版本:
2 修改工程Servlet版本配置信息,文件為:工作目錄\SessionExample\.settings\org.eclipse.wst.common.project.facet.core.xml
<?xml version="1.0" encoding="UTF-8"?> <faceted-project><runtime name="Apache Tomcat v6.0"/><fixed facet="java"/><fixed facet="wst.jsdt.web"/><fixed facet="jst.web"/><installed facet="java" version="1.7"/><installed facet="jst.web" version="2.5"/><installed facet="wst.jsdt.web" version="1.0"/> </faceted-project>getAttribute與getParameter的區(qū)別
這部分是對(duì)JSP的擴(kuò)展,經(jīng)常在JSP或者Servlet中獲取數(shù)據(jù),那么getAttribute與getParameter有什么區(qū)別呢?
1 從獲取到數(shù)據(jù)的來(lái)源來(lái)說(shuō):
getAttribtue獲取到的是web容器中的值,比如:
我們?cè)赟ervlet中通過(guò)setAttribute設(shè)定某個(gè)值,這個(gè)值存在于容器中,就可以通過(guò)getAttribute方法獲取;
?
getParameter獲取到的是通過(guò)http傳來(lái)的值,比如這樣一個(gè)http請(qǐng)求:
http:localhost:8080/test/test.html?username=xingoo還有其他的GET和POST方式,都可以通過(guò)getParameter來(lái)獲取。
2 從獲取到的數(shù)據(jù)類型來(lái)說(shuō):
getAttribute返回的是一個(gè)對(duì)象,Object。
getParameter返回的是,前面頁(yè)面中某個(gè)表單或者h(yuǎn)ttp后面參數(shù)傳遞的值,是個(gè)字符串。
轉(zhuǎn)載:http://www.cnblogs.com/xing901022/p/4378727.html
轉(zhuǎn)載于:https://www.cnblogs.com/sunxun/p/6626335.html
總結(jié)
以上是生活随笔為你收集整理的java Web监听器导图详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 做梦梦到蟾蜍什么意思
- 下一篇: The 15th UESTC Progr