?

Log in

Previous Entry | Next Entry

Session is the time when the client and server interacts with each other. A session starts when the end user starts the browser and end when the end user closes the browser. For example let’s say user starts a site www.xyz.com then the session starts. After that let’s say he starts shopping, checking emails and closes the browser. Once he closes the browser the session terminates. In simple words se rver application communicates with your client browser in a STATELESS way.

image1

Because of this nature of statelessness the end user who surfs your site can have undesirable results. For example take the example of the below interaction between server and client.  See the last step where the user gets undesirable results. He was expecting a report but he is redirected to the login page.

table2

So somehow we need to remind the server on which step or state we were , so that he can resume from that point. This is where we need to manage session.

Session management can be done by two ways:-

                    image3

  • Client session management (Cookies) :- In client session management the state is stored in the client browser in files called as cookies.  So for example you can set the cookie with value which page you visited using the below javascript code.

document.cookie="lastpagevisited=login";

Now every time client visits the server the cookie value is sent reminding what is the state of the client so that server can work as per the state of the client. To retrieve cookie using ASP.NET server side programming language we need to use “Request” object and use the “Cookie” function to retrieve the value using the key.

Request.cookie(“LastPageVisited”)

So now if the client cookie says “LastPageVisited” was “Login” then the server should probably send him the “HomePage”.

If (Request.cookie(“LastPageVisited”) == “login”
{

Response.redirect(“Home”);
}

  • Server side session management (Session variables)  :- Server side session management is done by using session variables. Session variables maintain data per user right from when the browser starts till the end. As soon as the browser is closed session variable is cleaned up. Below are the syntaxes to set session value and to get the value.

Session[“LastPage”] = “Login”;// to set session value
string strlastpage = Session[“LastPage”]; // get the session value

table3

You can also watch this youtube video which is created by www.questpond.comThis video explains the difference between session and viewstate.

Copy right www.questpond.com

Buy 600+ Real Time Interview Videos

Call +91-22-66752917 or mail us on questpond@questpond.com for more details on C#, .NET, ASP.NET, SQL Server interview question videos.

SQL Server interview question videos

Latest Month

March 2017
S M T W T F S
   1234
567891011
12131415161718
19202122232425
262728293031 

Tags

Powered by LiveJournal.com