java Web监听器导图详解
監(jiān)聽器是JAVA Web開發(fā)中很重要的內(nèi)容,其中涉及到的知識,可以參考下面導(dǎo)圖:
Web監(jiān)聽器
1 什么是web監(jiān)聽器?
web監(jiān)聽器是一種Servlet中的特殊的類,它們能幫助開發(fā)者監(jiān)聽web中的特定事件,比如ServletContext,HttpSession,ServletRequest的創(chuàng)建和銷毀;變量的創(chuàng)建、銷毀和修改等。可以在某些動作前后增加處理,實現(xiàn)監(jiān)控。
2 監(jiān)聽器常用的用途
通常使用Web監(jiān)聽器做以下的內(nèi)容:
統(tǒng)計在線人數(shù),利用HttpSessionLisener
加載初始化信息:利用ServletContextListener
統(tǒng)計網(wǎng)站訪問量
實現(xiàn)訪問監(jiān)控
3 接下里看看一個監(jiān)聽器的創(chuàng)建以及執(zhí)行過程
? 首先需要創(chuàng)建一個監(jiān)聽器,實現(xiàn)某種接口,例如我想實現(xiàn)一個對在線人數(shù)的監(jiān)控,可以創(chuàng)建如下的監(jiān)聽器:
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)聽器,在web-app中添加:
<listener><listener-class>com.test.MyListener</listener-class></listener>在JSP中添加訪問人數(shù):
<body>在線人數(shù):<%=session.getAttribute("userNumber") %><br/> </body>當(dāng)我使用我的瀏覽器訪問時,執(zhí)行結(jié)果如下:
當(dāng)打開另一個瀏覽器訪問時:
由于打開另一個瀏覽器訪問,相當(dāng)于另一個會話,因此在線人數(shù)會增加。
對于3.0版本的Servlet來說,還支持使用注解的方式進行配置。
那么接下來看看都有哪些監(jiān)聽器以及方法吧!
監(jiān)聽器的分類
1 按照監(jiān)聽的對象劃分:
按照監(jiān)聽對象的不同可以劃分為三種:
ServletContext監(jiān)控:對應(yīng)監(jiān)控application內(nèi)置對象的創(chuàng)建和銷毀。
當(dāng)web容器開啟時,執(zhí)行contextInitialized方法;當(dāng)容器關(guān)閉或重啟時,執(zhí)行contextDestroyed方法。
實現(xiàn)方式:直接實現(xiàn)ServletContextListener接口:
public class MyServletContextListener implements ServletContextListener{public void contextDestroyed(ServletContextEvent sce) {}public void contextInitialized(ServletContextEvent sce) {} }HttpSession監(jiān)控:對應(yīng)監(jiān)控session內(nèi)置對象的創(chuàng)建和銷毀。
當(dāng)打開一個新的頁面時,開啟一個session會話,執(zhí)行sessionCreated方法;當(dāng)頁面關(guān)閉session過期時,或者容器關(guān)閉銷毀時,執(zhí)行sessionDestroyed方法。
實現(xiàn)方式:直接實現(xiàn)HttpSessionListener接口:
public class MyHttpSessionListener implements HttpSessionListener{public void sessionCreated(HttpSessionEvent arg0) {}public void sessionDestroyed(HttpSessionEvent arg0) {} }ServletRequest監(jiān)控:對應(yīng)監(jiān)控request內(nèi)置對象的創(chuàng)建和銷毀。
當(dāng)訪問某個頁面時,出發(fā)一個request請求,執(zhí)行requestInitialized方法;當(dāng)頁面關(guān)閉時,執(zhí)行requestDestroyed方法。
實現(xiàn)方式,直接實現(xiàn)ServletRequestListener接口:
public class MyServletRequestListener implements ServletRequestListener{public void requestDestroyed(ServletRequestEvent arg0) {}public void requestInitialized(ServletRequestEvent arg0) {} }?
2 按照監(jiān)聽事件劃分:
2.1 監(jiān)聽事件自身的創(chuàng)建和銷毀:同上面的按對象劃分。
2.2 監(jiān)聽屬性的新增、刪除和修改:
監(jiān)聽屬性的新增、刪除和修改也是劃分成三種,分別針對于ServletContext、HttpSession、ServletRequest對象:
ServletContext,實現(xiàn)ServletContextAttributeListener接口:
通過調(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,實現(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,實現(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ài):
針對某些POJO類,可以通過實現(xiàn)HttpSessionBindingListener接口,監(jiān)聽POJO類對象的事件。例如:
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ǎng)站相關(guān)的重要信息,因此過多的session數(shù)據(jù)就會服務(wù)器性能的下降,占用過多的內(nèi)存。因此類似數(shù)據(jù)庫對象的持久化,web容器也會把不常使用的session數(shù)據(jù)持久化到本地文件或者數(shù)據(jù)中。這些都是有web容器自己完成,不需要用戶設(shè)定。
不用的session數(shù)據(jù)序列化到本地文件中的過程,就是鈍化;
當(dāng)再次訪問需要到該session的內(nèi)容時,就會讀取本地文件,再次放入內(nèi)存中,這個過程就是活化。
類似的,只要實現(xiàn)HttpSeesionActivationListener接口就是實現(xiàn)鈍化與活化事件的監(jiān)聽:
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ā)布該工程的,首先看一下版本不匹配時,會發(fā)生什么!
我試圖創(chuàng)建一個web工程,并且選取了Servlet3.0版本:
然后我想要在tomcat6中發(fā)布,可以看到報錯了!
JDK版本不對....這是在平時開發(fā)如果對Servlet不熟悉的web新手,常犯的錯誤。
解決方法:
1 在創(chuàng)建時,直接發(fā)布到Tomcat容器中,此時Servlet僅僅會列出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ū)別
這部分是對JSP的擴展,經(jīng)常在JSP或者Servlet中獲取數(shù)據(jù),那么getAttribute與getParameter有什么區(qū)別呢?
1 從獲取到數(shù)據(jù)的來源來說:
getAttribtue獲取到的是web容器中的值,比如:
我們在Servlet中通過setAttribute設(shè)定某個值,這個值存在于容器中,就可以通過getAttribute方法獲取;
?
getParameter獲取到的是通過http傳來的值,比如這樣一個http請求:
http:localhost:8080/test/test.html?username=xingoo還有其他的GET和POST方式,都可以通過getParameter來獲取。
2 從獲取到的數(shù)據(jù)類型來說:
getAttribute返回的是一個對象,Object。
getParameter返回的是,前面頁面中某個表單或者h(yuǎn)ttp后面參數(shù)傳遞的值,是個字符串。
轉(zhuǎn)載:http://www.cnblogs.com/xing901022/p/4378727.html
轉(zhuǎn)載于:https://www.cnblogs.com/sunxun/p/6626335.html
總結(jié)
以上是生活随笔為你收集整理的java Web监听器导图详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做梦梦到蟾蜍什么意思
- 下一篇: 梦到自己生孩子是什么预兆周公解梦