Session、Cookie和Application的区别

Session:能够保存相对少量的、简单的数据,这是面向单个用户、单个连接的信息、保存于服务器端(会占服务器端资源)。

当session超时或者被关闭时讲清空数据。由于用户停止使用应用程序后任然会存在一段时间(一般是20分钟),因此session对象保存数据的效率比较低,适用用于保存少量数据。

Cookie:保存少量的、简单的数据,一般不允许超过4KB(与浏览器有关),使用期限可以进行灵活设定,面向单个用户,保存于浏览器中(客户端)。而由于信息存储在客户端,最好不要保存敏感的信息.

Application:能够包含任意大小的信息,而在整个应用程序周期中有效,而面对所有应用程序用户的,保存于服务器端。当我们的网站访问量过大时,可能造成性能上的瓶颈。因此我们不推荐使用该对象来保存大的数据集合。

一个简单的登陆程序:

用户首次登陆用Cookie保存状态、Session保存用户名、Application计算访问量,用户第二次访问直接进入网站,不需要再登陆。

拖一个TEXTBOX和Button控件

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

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        用户名:<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="btnSubmit" runat="server" Text="登陆"
            onclick="btnSubmit_Click" />
    </div>
    </form>
</body>
</html>

打开后台代码:

using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Page.IsPostBack)
        {//在回传的时候读取Cookie的值
            //判断是否为空
            if (Request.Cookies["username1"] != null)
            {
                Session["username1"] = Request.Cookies["username1"].Value;
                //跳到指定页面
                Response.Redirect("Welcome.aspx");
            }
        }

    }

在aspx设计双击登陆按钮
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //写入Cookie
        Response.Cookies["username"].Value = txtUserName.Text;
        //设置Cookie保存时间
        Response.Cookies["username"].Expires = DateTime.Now.AddDays(1);
        Response.Redirect("Welcome.aspx");
    }
}

新建要登陆到的页面:

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

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:Label ID="labApplication" runat="server"></asp:Label>

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

创建一个全局应用程序集Global.asax文件

    void Session_Start(object sender, EventArgs e)
    {
        //在新会话启动时运行的代码
        Application.Lock();
        //全局计数器   计数+1
        int count = (int)Application["SessionCount"];
        Application["SessionCount"] = count + 1;
        Application.UnLock();

    }

打开后台代码:

public partial class Welcome : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Session["username"] != null)
        {
            string conent = "欢迎" + Session["username"] + "来到2010世界末日!";
            Response.Write(conent);
            if (Application["SessionCount"] != null)
            {
                labApplication.Text = Session["username1"]+"共死了"+Application["SessionCount"].ToString()+"次";
            }
        }

    }
}

在登陆页Ctrl+F5运行,首次填写用户名,跳转到下个页面出现如图结果:


原文地址:https://www.cnblogs.com/scsuns520/p/1631676.html