javaEE之-------統計網站重新整理量

來源:互聯網
上載者:User

標籤:final   說明   tco   啟動   知識   data-   add   click   integer   

每一個網站都有自己的統計訪問量,可是少不了server會出現意外情況,(如斷電。。)

所以就須要我們在網站掛機的前段時間將這個資料存起來。

我們就須要用到網站最大的容器,application,我們採用觀察者設計模式實現ServletContextListener介面。然後在銷毀之前將這個資料存起來

ps:屬於點點知識。大牛請繞道。

開發步驟:

第一步:實現ServletContextListener介面。
第二步:實現兩個方法。
contextInitialized
contextDestroyed

第三步:在web.xml中加入<listener/>節點。


詳細實現:

我們須要實現ServletContextListener介面,裡面用兩個方法。我們須要在初始化的時候從檔案中面讀出來。然後在銷毀的時候存進去。

讀取檔案:

public class MyServletContext implements ServletContextListener {//這是監聽器。統計網站的訪問量/* * 啟動的時候(初始化)從檔案中讀取。存在servletcontext中 * 銷毀的時候,把資料從servletcontext中取出來,存到檔案中 */String filename ="";@Overridepublic void contextInitialized(ServletContextEvent sce) {ServletContext context=sce.getServletContext();String filename=context.getRealPath("/count.txt");try {BufferedReader br =new BufferedReader(new FileReader(filename));String num =br.readLine();Integer numer =Integer.valueOf(num);context.setAttribute("count", numer);//將讀取的值存放到servletcontext容器中br.close();} catch( Exception e) {e.printStackTrace();context.setAttribute("count", new Integer(0));//出異常說明沒有值讀取,所以設定為0;}}

?

銷毀將資料存放區到檔案(僅僅有檔案才是永久儲存)

@Overridepublic void contextDestroyed(ServletContextEvent sce) {ServletContext context=sce.getServletContext();String filename=context.getRealPath("/count.txt");try {PrintWriter pw =new PrintWriter(filename);Integer count=(Integer) context.getAttribute("count");//從容器中擷取對應count值//pw.write(count);//存的檔案tomcat檔案夾下pw.print(count);System.out.println("銷毀了"+count);pw.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}

訪問量的加入

訪問量我們須要寫在過濾器裡面。每次過濾一次我們從context中加一次

public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {//這裡考慮到多線程,這樣寫資料不準確,所以須要採用多線程final ServletContext sct=request.getServletContext();//啟用新的線程去計數,不影響整個網站的速度,這樣做非常好new Thread(){public void run(){MyCount.addcount(sct);//把以下這個總體上鎖。。

}}.start();chain.doFilter(request, response);}

統計數量會存在多線程的問題。所以我們採用多執行緒,僅僅讓統計數量線程,不影響整個網站的效率

//把這個上鎖,僅僅會子分支會慢,不會影響整個網站的速度class MyCount{public synchronized static void  addcount(ServletContext sct){Integer count=Integer.parseInt(""+sct.getAttribute("count"));count++;//加入之後,我們須要加入容器裡面進去sct.setAttribute("count", count);System.out.println(count);}}
MyServletContext在初始化的時候會從檔案中載入,不存在會自己主動設定為一,每過一次filter。就會加1,這就實現了網站重新整理量的統計。

javaEE之-------統計網站重新整理量

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.