Session和Application实现网络在线聊天室实例

login.aspx代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sample_chat_login.aspx.cs" Inherits="Sample_chart_login" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css" >
        body { width:780px; margin:0px auto;}
        form { width:400px; margin:0px auto;}
        h3 { margin:10px; padding:10px; text-align:center;}
        p.tc { text-align:center; }
        
    
    </style>
</head>
<body>
    <form id="form1" runat="server" defaultbutton="Button1" defaultfocus="txt_id">
    <div>
        <h3>聊天室登录</h3>


        <div>
            <p class="tc">
                <span >用户名:</span>
                <asp:TextBox ID="txt_id" runat="server"></asp:TextBox>    </p>
        
            <p  class="tc">
                <asp:Button ID="Button1" runat="server" Text="登录聊天室" οnclick="Button1_Click" />
            </p>
        </div>
    
    </div>
    </form>
</body>
</html>

login.aspx.cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Sample_chart_login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //记录session: 当前用户名
        //跳转至聊天室页面
        if (txt_id.Text != "") {
            Session["s_id"] = txt_id.Text;
            Server.Transfer("Sample_chat_room.aspx");
        }
    }
}

room.aspx代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sample_chat_room.aspx.cs" Inherits="Sample_chat_room" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    
    <style type="text/css" >
        body { width:780px; margin:0px auto;}
       
        h3 { margin:10px; padding:10px; text-align:center;}
        p.tc { text-align:center; }
        
        #pnl_chat 
            { margin:10px; padding:10px;
              border:1px solid #dadada;
              height:300px;
                }
        #div_ctls
            { margin:10px; padding:10px;
              border:1px solid #dadade;
                }
    </style>


</head>
<body >
    <form id="form1" runat="server" defaultbutton="Button1" defaultfocus="txt_word">
    <div>
    <h3>简易聊天室</h3>
    
    <asp:Panel ID="pnl_chat" runat="server" ScrollBars="Vertical">
    </asp:Panel>
    
    <div id="div_ctls">
        <p>
        <asp:TextBox ID="txt_word" runat="server" Width="400"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="发送" οnclick="Button1_Click" />
        &nbsp;
            <asp:Button ID="Button2" runat="server" Text="刷新聊天记录"  />
         &nbsp;
            <asp:Button ID="Button4" runat="server" Text="清空" οnclick="Button4_Click"  />
        &nbsp;
            <asp:Button ID="Button3" runat="server" Text="退出聊天" οnclick="Button3_Click" />
        </p>

   <p>
        <span>选择我的颜色:</span>
        
        <asp:DropDownList ID="ddl_color" runat="server">
            <asp:ListItem Value="#666666">默认</asp:ListItem>
            <asp:ListItem Value="red">红色</asp:ListItem>
            <asp:ListItem Value="green">绿色</asp:ListItem>
            <asp:ListItem Value="blue">蓝色</asp:ListItem>
        </asp:DropDownList>


        </p>
    </div>


    </div>
    </form>
</body>
</html>

room.aspx.cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Sample_chat_room : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //检测session是否存在,如果没有session值,返回登录页面
        if (Session["s_id"] == "" || Session["s_id"] == null) {
            Response.Redirect("Sample_chat_login.aspx");
        }


        

            //如果还没有Application["chat"]则创建,如果有则写入panel
            if (Application["chat"] != null)
            {
                pnl_chat.Controls.Add((Panel)Application["chat"]);
            }
            else
            {
                Panel _pnl = new Panel();
                Application["chat"] = _pnl;
            }
        

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if(txt_word.Text !="") { // 注意:实际应用中,文本框是否为空,都应在前台进行检测;

        Label lab_name = new Label();
        lab_name.Text = Session["s_id"].ToString() + "[" + DateTime.Now.ToLongTimeString() + "]:";

        Label lab_word = new Label();
        lab_word.Style.Add("color", ddl_color.SelectedValue);
        lab_word.Text = txt_word.Text;

        Literal br = new Literal();
        br.Text = "<br/>";

        Panel _apppnl = (Panel)Application["chat"];
        _apppnl.Controls.AddAt(0, br);
        _apppnl.Controls.AddAt(0, lab_word);
        _apppnl.Controls.AddAt(0, lab_name);
            
        //_apppnl.Controls.Add(lab_name);
        //_apppnl.Controls.Add(lab_word);
        //_apppnl.Controls.Add(br);

        Application.Lock();
        Application["chat"] = _apppnl;
        Application.UnLock();

         

        //清空文本框
        txt_word.Text = "";

        pnl_chat.Controls.Add((Panel)Application["chat"]);
        

        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Session.Remove("s_id");
        Response.Redirect("Sample_chat_login.aspx");

    }
    protected void Button2_Click(object sender, EventArgs e)
    {

    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Application.Lock();
        Application.Remove("chat");
        Application.UnLock();

        Server.Transfer("Sample_chat_room.aspx");
    }
}



     



  • 5
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
实验:内置对象使用 一、实验目的 1、掌握各个内置对象的含义; 2、理解并熟练应用sessionapplication对象。 二、实验内容 1、设计聊天室,在聊天室中,需要通过JSP内置对象application来实时保存特定数量的当前聊天信息。 聊天室的设计包括:用户进行登录,选择聊天室,进行聊天,退出聊天室。 在聊天室中,用户只需输入一个用户名就可以进入聊天室,但是如果当前有人在使用该用户名,那么就必须换一个唯一的用户名。 具体要求:  用户登录成功后,程序会要求用户选择聊天室。可以不设置用户自行建立聊天室的功能,而且在聊天中途不能从一个聊天室切换到另一个聊天室。  进入聊天室后,用户可以从用户信息窗口看到该聊天室中所有用户的用户名,也可以在聊天窗口中看到随时更新的聊天信息。用户可以给所有人或某一个聊天用户发送公共的聊天信息,这个聊天内容大家都可以看到。用户也可以给某个用户发送私人的聊天信息,这种信息属于私聊信息,只有发送者和接收者可以看到。此外,聊天窗口还会出现一些系统公告,比如某某上站、某某离开等消息,另外用户还可以自己定义聊天信息和聊天用户信息刷新的时间间隔。  在用户单击“退出”按钮后,页面关闭,同时applicationsession中保存的信息都将丢失。 三、实验方法 1、用户登录信息使用request对象getParameter()方法得到用户登陆的一些信息; 2、公聊信息可以使用application对象,私聊信息使用session对象。 3、聊天的信息要不断刷新页面,使用户实时看到聊天信息。 4、用户退出时,有两种情况需要考虑:一是用户点击“退出”按钮,二是关闭浏览器,强制退出窗口,可查阅windows感知浏览器关闭的事件的相应方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值