http的內置對象 Session Application Global.aspx文件


http請求和響應是無狀態的。

客戶端向服務器發出請求,對於服務器來講,
代表客戶端的請求對象是HttpRequest..
 //HttpRequest對象表示的是客戶端對於服務器的請求,那麼把客戶端的請求封裝在HttpRequest對象中。
 HttpRequest request = this.Request;
 //HttpResponse表示的是服務器對於客戶端的響應。
 //HttpResponse response = this.response;
 Response.Write("客戶端的請求的虛擬路徑:"+this.Request.FilePath);
 Response.Write("客戶端的請求的物理路徑:"+this.Request.PhysicalPath);
 Response.Write("請求的參數值:"+Request.QueryString["code"]);

內置對象:
1.HttpRequest對象
2.HttpResponse對象
3.HttpCookie對象
  由服務器端發送給客戶端的信息,
  但是這個信息保留在客戶端的電腦中。
  關鍵點:由服務器產生,由客戶端保存。
  不同瀏覽器的Cookie是獨立的。
protected void Page_Load(object sender,EventArgs e)
{
    if(!this.IsPostBack)//表示不是會發的數據。
   {
 if(Request.Cookies["username"] !=null)
 {
 string username = Request.Cookies["username"].Value;
 this.TextBox1.Text = username;
 }
    }
}


protected void Button_Click(object sender,EventArgs e)
{
 if(this.CheckBox1.Checked)//選中的話就表示的是記住用戶名
       {
    HttpCookie cookie1 = new HttpCookie("username",this.TextBox1.Text);
    //Cookie有一個過期時間
    cookie1.Expires.AddMinutes(1);//AddMinutes表示的是過期的時間是一分鐘。
    //Cookie對象要發送給客戶端
    Response.Cookies.Add(cookie1);
       }
 //Session是保存在服務器端的,默認的會話時間是20分鐘。
 Session["user"]=this.TextBox1.Text;
 //跳轉到另外一個頁面
 Response.Redirect("chat.aspx");
}
4.Session
  -位置:保存在服務器端,安全性高
  -類型:任意類型
  -Session保存的信息不與其他用戶共享
  -在用戶會話期間可以記錄和監視其他用戶信息
  -當回話過期或終止時服務器會清楚Session對象

  protected void Page_Load(object sender,EventArgs e)
 {
    if(Session["user"] !=null)
 {
     this.Label1.Text = Session["user"].ToString();
     this.Label2.Text = Application["userVisit"].ToString();
     this.Label3.Text = Application["count"].ToString();
 }
 }

5.Application
  所有的用戶都可以訪問和設置  而Session表示的是單個的用戶訪問和設置

 Global.aspx文件
 -處理應用程序級時間的可選文件
 -在應用程序的根目錄下
 Application_Start  接受第一個請求是觸發
 Application_End    應用程序結束時觸發
 Session_Start      某用戶第一次訪問時觸發
 Session_End        某用戶退出應用程序時觸發
 protected void Application_Start(object sender,EventArgs e)
 {
    Application.Lock();
    Application["UserVisit"]=0; //初始化
    Application["count"] = 0;
    Application.UnLock();
 }
 protected void Session_Start(object sender,EventArgs e)
 {
    Application.Lock();
    Application["UserVisit"]=(int)Application["UserVisit"]+1;
    Application["count"] = (int)Application["count"]+1;
    Application.UnLock();
 }
  protected void Session_End(object sender,EventArgs e)
 {
    Application.Lock();
    Application["count"] = (int)Application["count"]-1;
    Application.UnLock();
 }


發佈了25 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章