daixj110

          2010年10月29日

          學(xué)習(xí)知識轉(zhuǎn)載

          用RMI實現(xiàn)基于Java的分布式計算
          http://developer.51cto.com/art/200906/130417.htm
          java內(nèi)存分配
          http://developer.51cto.com/art/201009/225071.htm
          內(nèi)存溢出解決辦法
          http://developer.51cto.com/art/200906/129346.htm

          java引用
          http://developer.51cto.com/art/200906/130447.htm
          linux上java環(huán)境搭建
          http://developer.51cto.com/art/200511/10736.htm

          設(shè)計模式
          http://lavasoft.blog.51cto.com/62575/d-11
          Java內(nèi)存模型中的三個代
          http://developer.51cto.com/art/200909/153154.htm

          Java垃圾回收機制淺析
          http://developer.51cto.com/art/200906/130855.htm

          20個開發(fā)人員非常有用的Java功能代碼
          http://developer.51cto.com/art/200905/124291.htm

          posted @ 2010-10-30 19:37 游龍! 閱讀(154) | 評論 (0)編輯 收藏

          詳解Listener監(jiān)聽Http Session

          Listener 是Servlet 的監(jiān)聽器,它可以監(jiān)聽客戶端的請求、服務(wù)端的操作等。通過監(jiān)聽器,可以自動激發(fā)一些操作,比如監(jiān)聽在線的用戶的數(shù)量。當(dāng)增加一個 Http Session時,就激發(fā)session Created(Http Session Event se)方法,這樣就可以給在線人數(shù)加1。常用的監(jiān)聽接口有以下幾個:

          Servlet Context Attribute Listener監(jiān)聽對Servlet Context 屬性的操作,比如增加、刪除、修改屬性。

          Servlet Context Listener監(jiān)聽Servlet Context 。當(dāng)創(chuàng)建Servlet Context 時,激發(fā)Context Initialized (Servlet Context Event sce)方法;當(dāng)銷毀Servlet Context 時,激發(fā)Context Destroyed(Servlet Context Event sce)方法。

          Http Session Listener監(jiān)聽Http Session的操作。當(dāng)創(chuàng)建一個Session時,激發(fā)session Created(Http Session Event se)方法;當(dāng)銷毀一個Session時,激發(fā)session Destroyed (Http Session Event se)方法。

          Http Session Attribute Listener監(jiān)聽Http Session中的屬性的操作。當(dāng)在Session增加一個屬性時,激發(fā)attribute Added (Http Session Binding Event se) 方法;當(dāng)在Session刪除一個屬性時,激發(fā)attribute Removed(Http Session Binding Event se)方法;當(dāng)在Session屬性被重新設(shè)置時,激發(fā)attribute Replaced(Http Session Binding Event se) 方法。

          下面我們開發(fā)一個具體的例子,這個監(jiān)聽器能夠統(tǒng)計在線的人數(shù)。在Servlet Context 初始化和銷毀時,在服務(wù)器控制臺打印對應(yīng)的信息。當(dāng)Servlet Context 里的屬性增加、改變、刪除時,在服務(wù)器控制臺打印對應(yīng)的信息。

          要獲得以上的功能,監(jiān)聽器必須實現(xiàn)以下3個接口:
          ◆HttpSessionListener
          ◆Servlet Context Listener
          ◆Servlet Context AttributeListener

          import javax.servlet.http.*;  
          import javax.servlet.*
           
          public class OnLineCountListener implements HttpSessionListener,  ServletContextListener,ServletContextAttributeListener  {  
          private int count;  
          private ServletContext context = null;  
          public OnLineCountListener()  {  count=0;  //setContext();  
          }
            //創(chuàng)建一個session時激發(fā)  
          public void sessionCreated(HttpSessionEvent se)  {  
          count
          ++;  setContext(se);  
          }
            //當(dāng)一個session失效時激發(fā)  
          public void sessionDestroyed(HttpSessionEvent se)  {  
          count
          --;  
          setContext(se);  
          }
            //設(shè)置context的屬性,它將激發(fā)attributeReplaced或attributeAdded方法  
          public void setContext(HttpSessionEvent se)  {  
          se.getSession().getServletContext().  setAttribute(
          "onLine",new Integer(count)); 
           }
            //增加一個新的屬性時激發(fā)  
          public void attributeAdded(ServletContextAttributeEvent event) 
           log(
          "attributeAdded('" + event.getName() + "', '" +  event.getValue() + "')"); 
           }
            //刪除一個新的屬性時激發(fā)  public void attributeRemoved(ServletContextAttributeEvent event) {  
          log("attributeRemoved('" + event.getName() + "', '" +  event.getValue() + "')");  }
            //屬性被替代時激發(fā)  
          public void attributeReplaced(ServletContextAttributeEvent event) {  
          log(
          "attributeReplaced('" + event.getName() + "', '" +  event.getValue() + "')");  
          }
            //context刪除時激發(fā) 
           public void contextDestroyed(ServletContextEvent event) {  
          log(
          "contextDestroyed()");  this.context = null;  }
            //context初始化時激發(fā)  
          public void contextInitialized(ServletContextEvent event) {  
          this.context = event.getServletContext();  log("contextInitialized()");  
          }
            
          private void log(String message) {  System.out.println("ContextListener: " + message);  
          }
            

          在OnLine Count Listener 里,用count代表當(dāng)前在線的人數(shù),OnLine Count Listener將在Web服務(wù)器啟動時自動執(zhí)行。當(dāng) OnLine Count Listener構(gòu)造好后,把count設(shè)置為0。每增加一個Session,OnLine Count Listener會自動調(diào)用 session Created(Http Session Event se)方法;每銷毀一個Session,OnLine Count Listener會自動調(diào)用session Destroyed (Http Session Event se)方法。當(dāng)調(diào)用session Created(Http Session Event se)方法時,說明又有一個客戶在請求,此時使在線的人數(shù)(count)加1,并且把count寫到Servlet Context 中。 Servlet Context 的信息是所有客戶端共享的,這樣,每個客戶端都可以讀取到當(dāng)前在線的人數(shù)。

          從作用域范圍來說,Servlet 的作用域有Servlet Context ,Http Session,Servlet Request.以上是Listener監(jiān)聽Http Session

          轉(zhuǎn)自:http://developer.51cto.com/art/200907/134756.htm

          posted @ 2010-10-30 18:12 游龍! 閱讀(278) | 評論 (0)編輯 收藏

          臟數(shù)據(jù)

          丟失修改:當(dāng)一個事務(wù)修改了數(shù)據(jù),并且這種修改還沒有還沒有提交到數(shù)據(jù)庫中時,另外一個事務(wù)又對同樣的數(shù)據(jù)進行了修改,并且把這種修改提交到了數(shù)據(jù)庫中。這樣,數(shù)據(jù)庫中沒有出現(xiàn)第一個事務(wù)修改數(shù)據(jù)的結(jié)果,好像這種數(shù)據(jù)修改丟失了一樣。
            臟讀:當(dāng)一個事務(wù)正在訪問數(shù)據(jù),并對數(shù)據(jù)進行了修改,而這種修改還沒有提交到數(shù)據(jù)庫中,這時,另一個事務(wù)也訪問這個數(shù)據(jù),然后使用了這個數(shù)據(jù)。因為這個數(shù)據(jù)是還沒有提交的數(shù)據(jù),那么另一個事務(wù)讀到的這個數(shù)據(jù)是臟數(shù)據(jù),依據(jù)臟數(shù)據(jù)所做的操作可能是不正確的。
            不可重復(fù)讀:在一個事務(wù)內(nèi),多次讀同一數(shù)據(jù)。在這個事務(wù)還沒有結(jié)束時,另一個事務(wù)也訪問該同一數(shù)據(jù),那么,在第一個事務(wù)中的兩次讀數(shù)據(jù)之間,由于第二個事務(wù)的修改,第一個事務(wù)兩次讀到的數(shù)據(jù)可能是不一樣的。


          hibernate中的臟數(shù)據(jù)
          http://developer.51cto.com/art/200906/129861.htm

          posted @ 2010-10-29 23:01 游龍! 閱讀(263) | 評論 (0)編輯 收藏

          <2010年10月>
          262728293012
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 西城区| 塔城市| 五台县| 江华| 桦南县| 临洮县| 阿克| 黔西| 比如县| 湖南省| 彝良县| 兴安县| 舒城县| 逊克县| 彭山县| 福建省| 云南省| 枣阳市| 磴口县| 洪雅县| 靖江市| 高邑县| 海伦市| 江口县| 平乡县| 东阿县| 科技| 双城市| 偃师市| 通海县| 噶尔县| 乌海市| 伊川县| 威信县| 新竹县| 合作市| 塔城市| 名山县| 于田县| 博湖县| 恩平市|