6 State Management And Http Protocol

NET Centric Computing · Unit 6

State Management and HTTP Protocol

Exam-focused notes for State Management and HTTP Protocol (NET Centric Computing, BIT351): what the TU syllabus asks and how it has actually been tested, with 5 solved past questions from this unit.

State Management and HTTP Protocol in short

Web pages are sent over HTTP, which forgets every request as soon as it ends. Web apps still need to remember who the user is, what is in a cart, and what was typed. State management techniques solve this.

State Management and HTTP Protocol: notes

Stateless HTTP protocol

HTTP is a stateless protocol. The server treats each request as new and keeps no memory of earlier requests from the same browser. After the response is sent, the connection data is forgotten.

This design keeps servers simple and fast, and lets many servers share the load. But it creates a problem. After a user logs in, the next request carries no proof of the login. A shopping cart would be empty on every page.

To manage state on stateless HTTP, the app can send identifying data with each request. It can also keep data on the server and link it to the user. The main techniques are:

  • Client side: cookies, hidden fields, query strings, local storage.
  • Server side: session state, application state, cache, database.

Cookies definition and uses

A cookie is a small piece of text data that the server sends to the browser. The browser stores it and sends it back with every later request to the same site. The server sets it with the Set-Cookie response header. The browser returns it in the Cookie request header.

Uses of cookies:

  • Keep a user logged in (authentication cookie).
  • Hold a session ID that points to data stored on the server.
  • Remember preferences such as language or theme.
  • Track visits and support analytics or advertising.
  • Store a shopping cart id.

Important cookie settings:

  • Expires or Max-Age: when the cookie is removed. Without it, the cookie is a session cookie and ends when the browser closes.
  • HttpOnly: JavaScript cannot read the cookie, which helps against XSS.
  • Secure: sent only over HTTPS.
  • SameSite: limits sending in cross-site requests, which helps against CSRF.

Limits: a cookie is small (about 4 KB), it travels with every request, and the user can edit or delete it. So never store passwords or secrets in it.

// write a cookie
Response.Cookies.Append("theme", "dark", new CookieOptions
{
    Expires = DateTimeOffset.UtcNow.AddDays(7),
    HttpOnly = true,
    Secure = true,
    SameSite = SameSiteMode.Lax
});

// read a cookie
string theme = Request.Cookies["theme"];

// delete a cookie
Response.Cookies.Delete("theme");

What this unit covers

  • Stateless HTTP protocol
  • Cookies definition and uses
  • Hidden fields purpose and usage
  • Session state management
  • State management techniques

State management techniques

2081 · 5 marks

How do you manage state on stateless HTTP? [5]

HTTP is a stateless protocol -- each request-response cycle is independent. The server retains no memory of previous requests from the same client. However, real web applications (shopping carts, login sessions, etc.) require state to be maintained across multiple requests. --- - The server sends...

Open this answer on its own page →

Hidden fields purpose and usage

2080 · 5 marks

When do you need hidden field? Explain with an example. [5]

A hidden field is needed when you want to store and transmit data between web pages (client and server) without displaying it to the user on the browser screen. 1. Maintaining State Information - HTTP is stateless, so hidden fields help carry data (like user ID, session token) from one page to an...

Open this answer on its own page →

Cookies definition and uses

2081.1 · 5 marks

Define cookies. What are the uses of hidden fields? [5]

Show solved answer

Cookies and Hidden Fields


Cookies (Definition)

A cookie is a small piece of text data that a web server sends to a client's browser, which the browser stores locally on the user's machine. When the browser makes subsequent requests to the same server, it automatically sends the stored cookie back, allowing the server to identify and remember the user across multiple requests.

Key characteristics of cookies:

  • Stored on the client side (user's browser/machine)
  • Have an expiry date/time (session cookies expire when browser closes; persistent cookies have a set expiry)
  • Associated with a specific domain and path
  • Set using the Set-Cookie HTTP response header
  • Sent back to the server via the Cookie HTTP request header

Example:

Set-Cookie: username=JohnDoe; expires=Fri, 31 Dec 2025 12:00:00 GMT; path=/

Common uses of cookies:

  • User authentication and session management
  • Storing user preferences
  • Tracking user behavior
  • Shopping cart persistence

Hidden Fields

A hidden field is an HTML form element (<input type="hidden">) that stores data within a web form but is not visible to the user on the browser screen. The data is submitted to the server when the form is submitted.

Syntax:

<input type="hidden" name="userID" value="1023">

Uses of Hidden Fields

UseDescription
State ManagementMaintain state information (e.g., user ID, session token) across multiple page requests without cookies
Passing Data Between PagesTransfer data from one page to another through form submission without displaying it to the user
Security Tokens (CSRF Protection)Store anti-CSRF tokens to validate that form submissions are legitimate
Tracking Form ProgressIn multi-step forms (wizards), carry forward data collected in previous steps
Storing Record IDsPass database record IDs (e.g., product ID, order ID) back to the server during form submission
Preserving Original ValuesStore original field values to detect changes made by the user before update

Key Difference

FeatureCookiesHidden Fields
StorageClient's browser/diskInside HTML form
VisibilityNot visible in pageNot visible on screen but visible in page source
PersistenceCan persist across sessionsOnly available during form submission
Sent to serverWith every HTTP requestOnly on form submission
Open this answer on its own page →

0 · 5 marks

Why do we need cookies and hidden fields? Explain. [5]

Note: The following answer is based on standard web technology concepts, as no specific curriculum notes were available for this topic. --- HTTP (HyperText Transfer Protocol) is a stateless protocol, meaning each request-response cycle is independent. The server does not remember any previous int...

Open this answer on its own page →

Session state management

2081.1 · 5 marks

What is the use of session state? Discuss about single page application. [5]

Session state is a server-side mechanism used in web applications to store and maintain user-specific data across multiple HTTP requests during a user's visit (session) to a website. Since HTTP is a stateless protocol, session state is used to overcome this limitation. Key uses include: - User Au...

Open this answer on its own page →