• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Servlet » Session » Session Tracking Using Cookies
Next →
← Prev

Session Tracking Using Cookies

By Dinesh Thakur

The Hypertext Transfer Protocol (HTTP) is the network protocol that the web servers and the client browsers use to communicate with each other. The HTTP is a stateless protocol. A client browser opens a connection and requests for a resource from the web server. The web server then responds with a requested resource and closes the connection.

After closing the connection, the web server does not remember any information about the client. So any next request from the same client will be treated as a fresh request without any relation to the previous request. This is what makes HTTP a stateless protocol. This protocol works fine for simple web browsing where each request typically results in an HTML file being sent back to the client. This is because web server does not need to know whether a series of requests came from the same client or from a different client. However, maintaining state is important in just about any web application. The appropriate example is a shopping cart where user select items, add them to their shopping cart and continue shopping. If you want to associate a shopping cart with a user over multiple page requests, then you need some method of maintaing state. One way of maintaing state is to use cookies.

Cookies are small textual files that a web server may send to the user’s web browser which in turn saves it to the hard disk (or if they are temporary in the web browser’s memory). The server sends the cookie to the browser in response to an initial request. Subsequent request sends the same cookie back to the server, which enables the server to identify these requests as being from the same client. By letting the server read information it send to the client previously, the site can provide visitors with a numbers of benefits such as,

• Saving login identity i.e. user names.

• Customizing sites : Many web sites now have pages where users can customize what they see when they arrive. After giving the user choice of layouts and color schemes, the site stores the preferences on the client’s computer through the use of cookies. The user can return to the site at any time and get the previously configured page.

• Identifying a user during e-commerce session.

• It let sites remember which topics interest certain users and show advertisements relevant to those interests.

• Frequent visitor bonuses.

• Bookmarks : Cookie let the use remember where he was when he last visited the site.

• Games : Cookie let remember the current or highest scores and present new

  challenges based on past answers and performance.

In the simplest form, cookies store data in the form of name-value pairs with certain additional attributes which are exchanged in the response and request headers. Each attribute/value pair is separated by a semicolon. The web servers send a cookie by sending the user-cookie response header in the following format

set-cookie: Name= Value; Comment= COMMENT; Domain= DOMAINNAME;

Max-age= SECONDS         ; Path= PATH; Secure; Version = 1*DIGIT

Here,

• Name is the name of the cookie.

• Value is the value this name can hold.

• Comment is an optional parameter that specifies the purpose associated with the cookie.

• Domain is an optional parameter that is used to specify the domain to which the cookie will be sent in future requests. By default, it is the host name of the domain that has sent the set-cookie header.

• Max-age is an optional parameter that specify how long (in seconds) the browser should keep the cookie before it expires.

• Path is an optional parameter that specifies the URL path for which the cookie

 is valid.

• The Secure parameter specifies whether the cookie should be sent only over a

  secure connection (HTTPS). By default its value is false.

In Java, cookies are created and manipulated via the javax.servlet.http.Cookie class. This class provides numerous methods. Some of the most commonly used ones are,

Method

Description

String getComment ()             

Returns the comment associated with the cookie.

String getDomain ()                

Returns the domain limitation associated with the Cookie.

int getMaxAge ()                     

Returns the maximum age allowed for this cookie.

String getPath ()

Returns the path limitation fort this servlet.

boolean getSecure ()               

Returns true if this cookie requires a secure connection.

String getName ()                    

Returns the name of the cookie.

String getValue ()                   

Returns the value of the cookie in string format.

void setComment

(String purpose)

Sets the comment that describe the cookie’s purpose.

void setDomain          

(String pattern)                                    

Specifies the domain within which the cookie should be presented.

void setMaxAge

    (int expiry)                      

Sets the maximum age of cookie in seconds.

void setPath  

(java.lang.String url)                                           

Specifies the path for the cookie to which the  client should return the cookie.

void setValue  

(java.lang.String newValue)                               

Assigns a new value to a cookie after the cookie is created.                       

                                            Some methods of Cookie class

You’ll also like:

  1. What is Session Tracking?
  2. HTTP Session with Cookies
  3. Session Management
  4. Steps for Sending Cookies
  5. Reading Cookies from the Client
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

SQL Tutorials

SQL Tutorials

  • SQL - Home
  • SQL - Select
  • SQL - Create
  • SQL - View
  • SQL - Sub Queries
  • SQL - Update
  • SQL - Delete
  • SQL - Order By
  • SQL - Select Distinct
  • SQL - Group By
  • SQL - Where Clause
  • SQL - Select Into
  • SQL - Insert Into
  • SQL - Sequence
  • SQL - Constraints
  • SQL - Alter
  • SQL - Date
  • SQL - Foreign Key
  • SQL - Like Operator
  • SQL - CHECK Constraint
  • SQL - Exists Operator
  • SQL - Drop Table
  • SQL - Alias Syntax
  • SQL - Primary Key
  • SQL - Not Null
  • SQL - Union Operator
  • SQL - Unique Constraint
  • SQL - Between Operator
  • SQL - Having Clause
  • SQL - Isnull() Function
  • SQL - IN Operator
  • SQL - Default Constraint
  • SQL - Minus Operator
  • SQL - Intersect Operator
  • SQL - Triggers
  • SQL - Cursors

Advanced SQL

  • SQL - Joins
  • SQL - Index
  • SQL - Self Join
  • SQL - Outer Join
  • SQL - Join Types
  • SQL - Cross Join
  • SQL - Left Outer Join
  • SQL - Right Join
  • SQL - Drop Index
  • SQL - Inner Join
  • SQL - Datediff() Function
  • SQL - NVL Function
  • SQL - Decode Function
  • SQL - Datepart() Function
  • SQL - Count Function
  • SQL - Getdate() Function
  • SQL - Cast() Function
  • SQL - Round() Function

Other Links

  • SQL - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW