Web監聽器導圖詳解

  監聽器是JAVA Web開發中很重要的內容,其中涉及到的知識,能夠參考下面導圖:html

Web監聽器

  1 什麼是web監聽器?java

  web監聽器是一種Servlet中的特殊的類,它們能幫助開發者監聽web中的特定事件,好比ServletContext,HttpSession,ServletRequest的建立和銷燬;變量的建立、銷燬和修改等。能夠在某些動做先後增長處理,實現監控。web

  2 監聽器經常使用的用途數據庫

  一般使用Web監聽器作如下的內容:瀏覽器

  統計在線人數,利用HttpSessionLisenertomcat

  加載初始化信息:利用ServletContextListener服務器

  統計網站訪問量session

  實現訪問監控app

  3 接下里看看一個監聽器的建立以及執行過程eclipse

   首先須要建立一個監聽器,實現某種接口,例如我想實現一個對在線人數的監控,能夠建立以下的監聽器:

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中配置該監聽器,在web-app中添加:

<listener>
      <listener-class>com.test.MyListener</listener-class>
  </listener>

  在JSP中添加訪問人數:

<body> 在線人數:<%=session.getAttribute("userNumber") %><br/>
</body>

  當我使用個人瀏覽器訪問時,執行結果以下:

  當打開另外一個瀏覽器訪問時:

  因爲打開另外一個瀏覽器訪問,至關於另外一個會話,所以在線人數會增長。

  對於3.0版本的Servlet來講,還支持使用註解的方式進行配置。

  那麼接下來看看都有哪些監聽器以及方法吧!

監聽器的分類

  1 按照監聽的對象劃分:

  按照監聽對象的不一樣能夠劃分爲三種:

  ServletContext監控:對應監控application內置對象的建立和銷燬。

  當web容器開啓時,執行contextInitialized方法;當容器關閉或重啓時,執行contextDestroyed方法。

  實現方式:直接實現ServletContextListener接口:

public class MyServletContextListener implements ServletContextListener{ public void contextDestroyed(ServletContextEvent sce) { } public void contextInitialized(ServletContextEvent sce) { } }

  HttpSession監控:對應監控session內置對象的建立和銷燬。

  當打開一個新的頁面時,開啓一個session會話,執行sessionCreated方法;當頁面關閉session過時時,或者容器關閉銷燬時,執行sessionDestroyed方法。

  實現方式:直接實現HttpSessionListener接口:

public class MyHttpSessionListener implements HttpSessionListener{ public void sessionCreated(HttpSessionEvent arg0) { } public void sessionDestroyed(HttpSessionEvent arg0) { } }

  ServletRequest監控:對應監控request內置對象的建立和銷燬。

  當訪問某個頁面時,出發一個request請求,執行requestInitialized方法;當頁面關閉時,執行requestDestroyed方法。

  實現方式,直接實現ServletRequestListener接口:

public class MyServletRequestListener implements ServletRequestListener{ public void requestDestroyed(ServletRequestEvent arg0) { } public void requestInitialized(ServletRequestEvent arg0) { } }

 

  2 按照監聽事件劃分:

  2.1 監聽事件自身的建立和銷燬:同上面的按對象劃分。

  2.2 監聽屬性的新增、刪除和修改:

  監聽屬性的新增、刪除和修改也是劃分紅三種,分別針對於ServletContext、HttpSession、ServletRequest對象:

  ServletContext,實現ServletContextAttributeListener接口:

  經過調用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,實現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,實現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 監聽對象的狀態:

  針對某些POJO類,能夠經過實現HttpSessionBindingListener接口,監聽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數據的鈍化與活化:

  因爲session中保存大量訪問網站相關的重要信息,所以過多的session數據就會服務器性能的降低,佔用過多的內存。所以相似數據庫對象的持久化,web容器也會把不常使用的session數據持久化到本地文件或者數據中。這些都是有web容器本身完成,不須要用戶設定。

  不用的session數據序列化到本地文件中的過程,就是鈍化;

  當再次訪問須要到該session的內容時,就會讀取本地文件,再次放入內存中,這個過程就是活化。

  相似的,只要實現HttpSeesionActivationListener接口就是實現鈍化與活化事件的監聽:

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官網給出的匹配:

  若是版本不匹配,那麼tomcat是不能發佈該工程的,首先看一下版本不匹配時,會發生什麼!

  我試圖建立一個web工程,而且選取了Servlet3.0版本:

  而後我想要在tomcat6中發佈,能夠看到報錯了!

  JDK版本不對....這是在平時開發若是對Servlet不熟悉的web新手,常犯的錯誤。

  解決方法:

  1 在建立時,直接發佈到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的區別

  這部分是對JSP的擴展,常常在JSP或者Servlet中獲取數據,那麼getAttribute與getParameter有什麼區別呢?

  1 從獲取到數據的來源來講:

  getAttribtue獲取到的是web容器中的值,好比:

  咱們在Servlet中經過setAttribute設定某個值,這個值存在於容器中,就能夠經過getAttribute方法獲取;

 

  getParameter獲取到的是經過http傳來的值,好比這樣一個http請求:

http:localhost:8080/test/test.html?username=xingoo

  還有其餘的GET和POST方式,均可以經過getParameter來獲取。

  2 從獲取到的數據類型來講:

  getAttribute返回的是一個對象,Object。

  getParameter返回的是,前面頁面中某個表單或者http後面參數傳遞的值,是個字符串。

 

參考

  【1】慕課網,監聽器:http://www.imooc.com/learn/271

  【2】jsp中getAttribute與getParameter的區別:http://wenku.baidu.com/link?url=4URJWerrusLTFRviR1sAlTH4BKc7QswiRYsso3xaYs_nZMiTMV-TwCnIIgu31K1N9HbrUhfgO2-jXjpYe1hGZn9RBo3b8HHzY2Dn2-Fcbs7

相關文章
相關標籤/搜索