You are on page 1of 4

AACS4134 Internet Programming

Tutorial 6 (Solution) 1. Suggest the mechanisms that can be used in a Web application to maintain state data. Provide appropriate examples to aid your explanation. A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. The Session object is used to store information about, or change settings for a user session. Variables stored in the Session object hold information about one single user, and are available to all pages in one application. Common information stored in session variables are name, id, and preferences. The server creates a new Session object for each new user, and destroys the Session object when the session expires. The Application object is used to store and access variables from any page, just like the Session object. The difference is that ALL users share one Application object, while with Sessions there is one Session object for EACH user. The Application object should hold information that will be used by many pages in the application (like database connection information). This means that you can access the information from any page. It also means that you can change the information in one place and the changes will automatically be reflected on all pages. Source: http://www.w3schools.com/ASP/asp_sessions.asp 2. We can use the followings variables in a Web application: application variables session variables (a) Describe the differences between each of them

Application variables can be accessed and viewed by all users Session variables cant be accessed by any other users other than the one who belongs to the session. (a) Discuss the pros and cons for each of the object variables above.

Application variables Since Application state variables are global to an application, it is important to consider following issues while storing the value : The memory occupied by variable is not released until the value is either removed or replaced. Therefore number of variable and objects in application state should be minimumm and Explicit synchronization method should be used to avoid deadlocks and access violations, because many pages can access values stored in an application state simultanesouly. Advantages : Easy to use and is consistent with other .net framework classes, and Storing information in application state involves maintaining a single copy of information.

Chapter 8 Users and Applications

AACS4134 Internet Programming

Disadvantages : Data stored in application state is lost when web server fails due to server crash, upgrade or shut down, and Request server memory and can affect the performance of server. Session variables Pros:

Session state is event driven. Therefore you can use session events to perform conditional execution of user defined tasks, and Session state can survive web server restart without losing session data, because data is stored in another process. If you have a variable that needs to be passed around to a lot of web pages, it may simplify things to use a Session variable, rather than passing the variable around through the QueryString. Session variables allow for customization of a web site. Each visitor to your site can have a customized experience. While this is true, it is also true that with the advent of LDAP and items such as MS Site Server and the like, it is no longer necessary to put all of your customization that is dependent upon user preference into session variables. Session variables take you one step closer to VB programming in the sense that you can grab one without initializing the variable, use it whenever you want to, and not have to worry about releasing it when you've finished using it.

Cons:

Stored in memory until they are either removed or replaced, which can degrade the Web server performance. Session variables and cookies are synonymous. So if a user has set his browser not to accept any cookies, your Session variables won't work for that particular web surfer! An instance of each session variable is created when a user visits the page, and these variables persist for 20 minutes AFTER the user leaves the page! (Actually, these variables persist until they "timeout". This timeout length is set by the web server administrator. I have seen sites that the variables will collapse in as little as 3 minutes, and others that persist for 10, and still others that persist for the default 20 minutes. ) So, if you put any large objects in the Session (such as ADO recordsets, connections, etc.), you are asking for serious trouble! As the number of visitors increase, your server will experience dramatic performance woes by placing large objects in the Session! Since Session variables can be created on the fly, used whenever, and do not require the developer to dispose of them explicitly, the overuse of Session variables can lead to very unreadable and unmaintainable code. Each session variable is separate in memory from all of the others. Therefore clean up of the variables becomes the major problem. When the user logs out, it is possible that one or more of the variables do not get removed as they should. Session variables take you one step closer to VB programming in the sense that you can grab one without initializing the variable, use it whenever you want to, and not have to worry about releasing it when you've finished using it. And WHO wants to go there? Not me. &

Sources: http://www.4guysfromrolla.com/webtech/092098-2.shtml http://dotnetquestionsanswers.blogspot.com/

Chapter 8 Users and Applications

AACS4134 Internet Programming

3. A cookie is often used to identify a user and the Session object is used to store
information about a user session. Explain clearly the differences between a cookie and a session variable. Cookies A cookie is used to store small piece of information on client machine. Information can be kept for a long period of time. The downside is that cookies can be blocked by some security settings. Cookies are saved on client computer and can either be Temporary or Persistent. Temporary cookies, also known as session cookies exists in memory space of browser. When browser is closed , all session cookies added to browser will be lost. Persistent Cookies, is saved as a text file in a file system of client computer. Advantages : Easy to create and maintain, No server resources is involved in maintainance of cookie and Either expires when browser session ends or exists indefinetly in client computer Disadvantages : They have limited size and most browser alllows cookies to have maximum 4096 bytes in size, Can be tampered because cookies are stored in client computer and User can disable cookies.

Session information is stored in server memory, expired/destroyed when


the session ends/connection times out.

4. You have developed a Web application that allows the user to buy items from different
pages in an online catalog. Justify which object variables: session variables or application variables that you would use in the Web application? Explain your answer. If you have a variable that needs to be passed around to a lot of web pages, it may simplify things to use a Session variable. Since Application state variables are global to an application, hence this is not appropriate as all users will update the items information globally, rather than different user having different purchase. While Session variables allow for customization of a web site. Each visitor to your site can have a customized (individual) experience.

5. Write a button click event handler to store a user ID entered by a user in a cookie file that last for 30 days. Also write a Page Load event handler that display the user ID retrieved from the cookie file into a label control. protected void Page_Load(object sender, EventArgs e) { if(Request.Cookies["userID"].Value!=null); lblCookie.Text = Request.Cookies["userID"].Value; } protected void Button1_Click(object sender, EventArgs e) { HttpCookie myCookie = new HttpCookie("userID"); myCookie.Value = txtName.Text; myCookie.Expires = DateTime.Now.AddDays(30); Response.Cookies.Add(myCookie);

Chapter 8 Users and Applications

AACS4134 Internet Programming

} 6. Consider the following code snippet in a Global.asax file 1 void Session_Start(object sender, EventArgs e) 2 { 3 Application.Lock(); 4 Application["UserCount"] =(int)Application["userCount"] + 1; 5 Application.UnLock(); 6 } (a) Discuss the importance of locking the application as shown in line 3.

You can lock an application with the "Lock" method. When an application is locked, the users cannot change the Application variables (other than the one currently accessing it).

(b)

When the Web application starts, the number of user (UserCount) is set to zero (0). Demonstrate how this can be done by inserting the appropriate codes in the Global.asax file.

void Application_Start(object sender, EventArgs e) { Application["userCount"] = 0; } (c) Based on the given Global.asax above, demonstrate by writing a line of C# code, to display the following example of message on a label named lblCount on the home page. Number of visitors: 1000 (Remark: 1000 is retrieved from the Global.asax file) lblCount.Text = Number of visitors: + Application[userCount].ToString();

7. Differentiate between output caching and fragment caching.


Output caching will cache the entire page but fragment chaching will cache the content from the user control only.

8. Write the codes to retrieve the information based on the URL given below: Response.Redirect(Myselection.aspx?selection=abc);
Label1.Text = Request.QueryString[selection];

Chapter 8 Users and Applications

You might also like