Application,session,cookie, can you tell me the point? --"ASP."

Source: Internet
Author: User

background:

Every thing produced by the time is a reason drops, I personally think, these methods are similar to the cache, is to put some of the commonly used data into the cache to save up, than directly from memory or hard disk read more quickly, thereby improving efficiency. Application,session,cookie is the three commonly used methods for accessing temporary data in ASP.


Application/session/cookie Comparison:

no

TD style= "BORDER-STYLE:SOLID; Border-color: #A3A3A3; border-width:1pt; Vertical-align:top; width:2.6333in; padding:4pt 4pt 4pt 4pt ">

Contrast

Session

Application

Cookies

function

Private information for protecting users

for saving public data information for all users.

request information to protect client browser request Server Pages

Use the

multiplayer

global

single

Share

no

yes

Validity

Individual Exit - minutes (can be set), low data storage efficiency

Program Cycle,

Will disappear, consider writing to a file or database

A performance bottleneck has been visited by the conference

Custom, close browser disappears

Data

Little, simple, single user, single link

any

Little, simple, non-sensitive

Position

Server-side

Server-side

Client





Landing Combat Walkthrough:

User login using a cookie to save user information, session save user name, application calculate the amount of access. The whole business logic is that the user first logged in, logged in and then entered a new page, showing the number of people logged in and the name of the person who landed.


Login.aspx.cs

using System;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;using system.security;using system.web.ui.htmlcontrols;using system.web.ui.webcontrols.webparts;using system.xml.linq;namespace demo5{public partial class LOGIN:SYSTEM.WEB.UI.P Age {//Log on login button protected void btnSubmit_Click (object sender, EventArgs e) {//write Cook IE response.cookies["username1"].            Value = txtUsername.Text; Set the cookie save time response.cookies["UserName1"].            Expires = DateTime.Now.AddDays (1); Determines whether the user name is empty if (request.cookies["username1"]! = null) {//session Gets the user name session["use Rname1 "] = request.cookies[" username1 "].                Value;            Jump to welcome page Response.Redirect ("welcome.aspx"); }        }    }}


Login.aspx

<%@ page language= "C #" autoeventwireup= "true" codebehind= "Login.aspx.cs" inherits= "Demo5. Login "%><! DOCTYPE html>


Results page:


To jump to a new page:

Welcome.aspx.cs

Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;namespace demo5{public    partial class Welcome:System.Web.UI.Page    {        //Load Welcome page        protected void Page_Load (object sender, EventArgs e)        {            //If the user name inside the Session is empty if            (session["username1"]! = null)            {//print string                conent = "Welcome" + session[" UserName1 "] +" Enter ASP.                " Response.Write (conent);                If the global variable is not empty if                (application["Sessioncount"]! = null)                {                    Labapplication.text = session["username1"] + " Total landed "+ application[" Sessioncount "]. ToString () + "Times";}}}}           

Welcome.aspx

<%@ page language= "C #" autoeventwireup= "true" codebehind= "Welcome.aspx.cs" inherits= "Demo5. Welcome "%><! DOCTYPE html>


Global.asax

        protected void Application_Start (object sender, EventArgs e)        {                       //Global Counter Count +1            application["sessioncount"] = 0;                    }        protected void Session_Start (object sender, EventArgs e)        {            session["username1"] = 0;            Code Application.Lock () that runs when the new reply is started            ;            Global Counter count +1            application["sessioncount"] = (int) application["Sessioncount"] + 1;            Application.UnLock ();        }


Login Success Page:



Phenomenon Analysis:

1. If you log in, then open a browser page, browse the same URL, the number of landing will be +1, indicating that application throughout the life cycle of the program, and can be accessed by multiple users, to save public user data information.

2. Sometimes, close the browser, restart the program immediately, the user name is still the previous user name, that the session is time-sensitive.


Summary:

Do the demo time is not very smooth, but after the completion is very fulfilling, 80 of the time is really more than 100 of the benefits.





Application,session,cookie, can you tell me the point? --"ASP."

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.