iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 12
0
自我挑戰組

30 days JSP & Servlet學習紀錄 系列 第 12

[Day 12] Listener 練習 -1

前言

前兩天都再整理書裡面的重點
接下來這幾天會針對這些特性
去做一些簡單的練習
今天先練習Listener的部分


練習:線上人數統計

這個練習使用到前面提到八種Linstener的HttpSessionListener
來實現統計現在有多少人在web上

  • 1.新增一支Listener
    在專案上面點右鍵新增一支Linstener
    一樣先輸入對應的package和class name
    http://ithelp.ithome.com.tw/upload/images/20161212/20103425uQByRcxNow.png
    再來是選擇所要新增的Listener
    此範例選擇的是HttpSessionListener
    http://ithelp.ithome.com.tw/upload/images/20161212/20103425qiK4FBW993.png
    按照步驟新增完default的程式碼如下圖
    http://ithelp.ithome.com.tw/upload/images/20161212/20103425yK4RtPWUhK.png
    在這裡先新增一個參數,負責統計線上的人數
public class myListener implements HttpSessionListener {
	 private static int tSessions;
    /**
     * Default constructor. 
     */
    public myListener() {
        // TODO Auto-generated constructor stub
    }
    public static int GetSessions() {
        return tSessions;
      }
	/**
     * @see HttpSessionListener#sessionCreated(HttpSessionEvent)
     */
    public void sessionCreated(HttpSessionEvent arg0)  { 
         // TODO Auto-generated method stub
    	tSessions++;
    }

	/**
     * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
     */
    public void sessionDestroyed(HttpSessionEvent arg0)  { 
         // TODO Auto-generated method stub
    	tSessions--;
    }

除了新增一個tSessions之外
還要新增一個method,讓Servlet可以去取得目前的值
並且在sessionCreated和sessionDestroyed分別去做加減

sessionCreated =>當client增加的時候會呼叫此method
sessionDestroyed =>當cilent離開session時呼叫此method

  • 2.設定DD(web.xml)
    在web.xml裡面,只需要註冊這個Listener
    因為Listener是for整個web所使用,所以要寫在web-app的tag裡
    web.xml
  <listener>
	<listener-class>com.web.Listener.myListener</listener-class>
  </listener>
</web-app>
  • 3.修改Servlet & JSP
    再來就是要在執行的Servlet裡面去取得這個listener回傳的參數值,在設定到JSP上呈現
    LoginServlet.java
        //在裡只驗證密碼和DD裡面的是否相同就好,來模擬多人在同個web的case
	    if(secretPassword.equals(password) )
		{
			request.setAttribute("myname",name);
            //呼叫GetSessions,回傳參數值並透過setAttribute到新增的cont給JSP使用
			request.setAttribute("cont",myListener.GetSessions()); 
			request.getRequestDispatcher("Index.jsp").forward(request, response);
			return;
		}
		else
		{
			//也可以在Servlet取出context-param,再由JSP取出
			//request.setAttribute("strMsg",strError);
			request.getRequestDispatcher("ErrorPage.jsp").forward(request, response);
		}

Index.jsp

<body>
<h1>Hi! <%= request.getAttribute("myname") %>.</h1>   this is my first Servlet!!!
there have <%=request.getAttribute("cont") %> people online!!
</body>

4.demo
IE
http://ithelp.ithome.com.tw/upload/images/20161212/20103425ZGaTp8Ntwh.png

開chrome登入的結果
http://ithelp.ithome.com.tw/upload/images/20161212/20103425HdVEMpeaeA.png

firefox
http://ithelp.ithome.com.tw/upload/images/20161212/20103425VkBFdeN2eB.png

小結

此範例是一個很簡單的練習
但還有一些問題
這部份明天會再紀錄上來


上一篇
[Day 11 ] Filter 簡介
下一篇
[Day 13] Filter 練習
系列文
30 days JSP & Servlet學習紀錄 30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言