Simple Chat Room program written by ASP. NET using application and session Object
ASP. Net has two important objects: application Object and session object. Application: the object that records Application parameters. This object is used to share Application-level information. Session: record the variable objects on the browser, used to store variables or objects of cross-page programs. To be honest, I wrote asp.net for almost a year, and the application object has never been used. I read the book and wrote a simple chat room program based on the characteristics of the two objects. It is really very simple. The general idea is that when logging on to the Default page, the entered nickname is saved to the session object, and the ChatRoom page is jumped to, and the session key is "nickname" is saved to the application object. Traverse the application object and add the nickname of the online person to The listbox on the left. The appliaction object is used in the Session_Start and Session_End methods to + 1 for the key "count", and-1 is used to record the current number of online users. Use the application Object key "content" to store chat records. The following code is Global. asax: protected void Application_Start (object sender, EventArgs e) {Application ["count"] = 0; Application ["content"] = "Chat History \ n ";} protected void Session_Start (object sender, EventArgs e) {Application. lock (); 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 ();} Default. aspx: protected void button_login_Click (object sender, EventArgs e) {if (! String. isNullOrEmpty (textbox_nickname.Text) {Session ["nickname"] = textbox_nickname.Text;} Response. redirect ("ChatRoom. aspx ");} ChatRoom. aspx: protected void Page_Load (object sender, EventArgs e) {label_count.Text = "the current number of online users is" + Application ["count"]. toString () + "person"; if (! IsPostBack) {if (Session ["nickname"]! = Null) {Application. add (Session ["nickname"]. toString (), Session ["nickname"]);} textbox_records.Text = Application ["content"]. toString ();} listbox_usernames.Items.Clear (); foreach (string str in Application. contents) {if (! Str. Equals ("content ")&&! Str. Equals ("count ")&&! Str. equals ("name") {listbox_usernames.Items.Add (new ListItem (Application [str]. toString (), Application [str]. toString () ;}}} protected void button_send_Click (object sender, EventArgs e) {if (Session ["nickname"]! = Null) {Application ["content"] + = (Session ["nickname"]. toString () + "said:" + textbox_sendmsg.Text + "\ n"); textbox_records.Text = Application ["content"]. toString () ;}} protected void button_exit_Click (object sender, EventArgs e) {Application. remove (Session ["nickname"]. toString (); Application ["count"] = (int) Application ["count"]-1; Response. redirect ("WebCounter. aspx ");} This simple chat room is complete. It seems that no new session will be created in browsers. You need to log on to different browsers to see the effect. Note that when you exit the chat room, you need to remove the value of the application key "nickname", that is, the current session ["nickname. Otherwise, when you refresh the listbox, you will find that the exit nickname still exists. At the same time, you also need to set the key to "count" of the application Object-1. Maybe you want to ask, isn't it already-1 in the Session_End method? I'm not sure about this. There may be two reasons. I don't know which one is it? Hope you can give me some advice. 1. The session has not expired. I just killed the corresponding key in the application as the session object, but I did not clear the real session. Therefore, the Session_End method is not called. (But you may ask, isn't it OK if you leave the current session ["nickname"] object empty? I tried this, but the count does not have-1 .) 2. Is it because I am using two local browsers to perform this chat? In some special cases, the application object is very helpful to us, so I don't need to talk about the session. It is definitely something we often use.