日期: 2023.10.19 星期二

方法 3. SESSION

 像是一個全域變數,放在 Server,資料型態是物件。Session 的資料保存於伺服器端,但是預設只能保留 20 分鐘,若 20 分鐘之內,未再連線,伺服器將自動刪除該資料,避免一大堆 Session 資料,存放於伺服器端,癱瘓機器。

  1. 新增 ASP.NET 專案後,加入三表單 WebForm1、WebForm2、WebForm3

    • WebForm1:

      • 介面: TextBox 1、Button 1
      • 功能: 按下 Button,傳出 TextBox1.Text 的值,給 WebForm2
      • 程式:

        protected void Button1_Click(object sender, EventArgs e){
          //使用 Query String 方法
          //Response.Redirect("WebForm2.aspx?myID=" + TetBox1.Text );
          Session["myID"] = TextBox1.Text;
          Response.Redirect("WebForm2.aspx");
    • WebForm2

      • 介面: Label * 1
      • 功能: 顯示 WebForm1.aspx.cs 傳送過來的值,並顯示
      • 程式:

        “`C#
        protected void Page_Load(object sender, EventArgs e){
        //使用 Query String 方法
        //string aaa = Request.QueryString["myID"]);
        //Label1.Tet = aaa;
        Label1.Text = Session["myID"].ToString();
        }

  2. 如何知到哪一個 Session 是哪一個 WebForm 的 Connection。
    Session 有一屬性叫做 Session ID,每一次使用者提出一 Request 要求伺服器去執行 WebForm,此刻瀏覽器會傳一個 Sessin ID 給伺服器,伺服器會記下此 ID,當該 WebForm 執行完畢,它會把這 Session ID 預設方法透過 Cookie 把 Session ID 下載下來給你,所以下一次上傳時又把同一 ID 丟上去,所以伺服器就會知道是哪一個 Session ID 跟我做連線,

    實驗

    1. 在 WebForm2 上多增加一 Label2,用來顯示 Session["ID"] 的值 。
    2. 在 WebForm2 上增加一 Button1,用來連結到 WebForm3。
    3. 在 WebForm3 上建立 Label * 2 (WebForm1 上的資料與其 SessionID)
    4. 在 WebForm2 上,Label2.Text 顯示 SessionID (透過瀏覽器送來的 SeeionID)

      “`C#
      protected void Page_Load(object sender, EventArgs e){
      //使用 Query String 方法
      //string aaa = Request.QueryString["myID"]);
      //Label1.Tet = aaa;
      Label1.Text = Session["myID"].ToString();
      //增加
      Label2.Text = Session.SessionID;
      }

    5. 在 WebForm2 上,按下按鈕時,跳轉到 WebForm3

      protected void Button1_Click(object sender, EventArgs e){
        Response.Redirect("WebForm3.aspx");
    6. 在 WebForm3 於 Page_Load 事件時,於 Label1.Text 顯示 WebForm1 傳的值

      “`C#
      protected void Page_Load(object sender, EventArgs e){
      Label1.Text = Session["myID"].ToString();
      //在顯示一次 SessionID
      Label2.Text = Session.SessionID;
      }

    7. 測試

      1. 執行 WebForm1,TextBox1 輸入 abc,按下按鈕。
        結果:

        abc
        sdasdsasdsaafasfasfasfa # SessionID
      2. 按下現在畫面的 WebForm2 按鈕,連結到 WebForm3
        結果:

        abc
        sdasdsasdsaafasfasfasfa # SessionID

        結論
        同樣的 SessionID,若不再使用這網站則該 SessionID 將會自動被刪除。

    8. SesionID 預設使用 Cookie 技術來做傳遞,但若關閉了 Cookie 就無法使用。
      所以可使用 URI 類似 Query String 的方式。

      1. 專案中找到 Web.config,修改為不用 Cookie 方式傳遞,並且每個 Session 的值,只保留 1 分鐘:

        :
        <system.web>
          <sessionState cookieless="UseUri" timeout="1"></sessionState>
        
        </system.web>
        :
      2. 在開啟 WebForm1,網址會出現訊息: localhost:50817/(S(xxxxxxxxxxxx))/WebForm1.aspx
        (S(xxxxxxxxxxxx)) 裡的 xxxxxxxxxxx,即 SessionID,透過 URI 的方式,上傳給 Server,此刻按下 WebForm1 的按鈕,就會也看到 Label1 上顯示相同的 SessionID
    9. 先移除專案中所修改 Web.config 的部分

方法 4. Application

Application 與 Session 使用方法相同,差異在於任何一個網頁連結到的 Application 的物件是同一個。
所以沒有所謂不同的 SessioID 問題,大家連線進來都是同一物件,該物件產生時機是 IIS 啟動時,關閉 IIS 就消失了。

  1. 應用: 設計一個計數器,看有多少了逛過該網頁。
  2. 步驟:

    1. 增加一 WebForm4:

      • 介面: Label1 * 1 存放次數、Button * 1 執行 PostBack
      • 功能: 按下 Button,傳出 TextBox1.Text 的值,給 WebForm2
    2. 若用 Session 方法來做
      程式:

      protected void Button1_Click(object sender, EventArgs e){
        int counter;
        if (Session["count"] == null)
          counter =1;
        else{
          counter = (int)Session["counter"] + 1;
        }
       Session["count"] = counter;
        Label1.Text = counter.ToString();
      }

      執行 WebForm4 後,按下 Button1,Label1.Text 的值會自動加一,沒有問題(假設為 10)。
      但模擬另一個使用者打開瀏覽器,連接這網址,按下 Button 會發現是從 1 開始,原因是使用了 Session 技術,每一個使用者連到那個伺服器的 SessioID 都不同所造成。所以不能用 SESSION 技術。

    3. 改成 Application 物件的方法

      protected void Button1_Click(object sender, EventArgs e){
        int counter;
        if (Application["count"] == null)
          counter =1;
        else{
          counter = (int)Application["counter"] + 1;
        }
       Application["count"] = counter;
        Label1.Text = counter.ToString();
      }
    4. 使用 Application 物件時,須考量到不同使用者操作若同時按下時,資料可能會亂掉。
      建議修改前作 Lock 動作 (Application.Lock()),不要讓其他使用者同一時間作修改,

      protected void Button1_Click(object sender, EventArgs e){
        int counter;
        Application.Lock();
        if (Application["count"] == null)
           counter =1;
        else{
           counter = (int)Application["counter"] + 1;
        }
      
        Application["count"] = counter;
        Application.UnLock();
        Label1.Text = counter.ToString();
      }
最後修改日期: 2023 年 11 月 22 日

作者

留言

撰寫回覆或留言

發佈留言必須填寫的電子郵件地址不會公開。