0% found this document useful (0 votes)
307 views2 pages

A Research About Cookies and Session PDF

This document discusses cookies and sessions. Cookies are small text files stored in the browser that contain user information like names and passwords. Sessions store similar user data on the server. The key difference is that cookies are stored locally on the client side browser, while sessions are stored on the server side. Cookies have limited storage capacity and can be deleted, while sessions have no storage limit but end when the user logs out or deletes cookies from their browser. The document provides code examples for how to use cookies to save and retrieve user name and ID data, and how to use sessions to save and display a user's name across multiple pages of a website.

Uploaded by

Phúc Plus Plus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
307 views2 pages

A Research About Cookies and Session PDF

This document discusses cookies and sessions. Cookies are small text files stored in the browser that contain user information like names and passwords. Sessions store similar user data on the server. The key difference is that cookies are stored locally on the client side browser, while sessions are stored on the server side. Cookies have limited storage capacity and can be deleted, while sessions have no storage limit but end when the user logs out or deletes cookies from their browser. The document provides code examples for how to use cookies to save and retrieve user name and ID data, and how to use sessions to save and display a user's name across multiple pages of a website.

Uploaded by

Phúc Plus Plus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Nguyen Hoang Phuc

ITITIU11008

A research about Cookies and Session


First thing first, we need to understand clearly about Cookies and Session. What we will be
covering in this research:

What is Cookie ?
What is Session ?
What is the difference between Cookie and Session"
How to use Cookie ?
How to use Session ?

What is Cookie ?
Cookie is not a cookie you eat, it is a tiny text file that store the info of user in their PC such as
name, password, address, email, phone number
Each time the internet browser interacts with a website it will pass the cookie to the website
server. Only the cookies stored by the internet browser that relate to the domain in the
requested link will be sent to the server.
If user use the internet browser with Private Mode (Internet Explorer), Anonymous Mode
(Firefox), Incognito Mode (Google Chrome) is on then any input will not be stored, its
recommended when using a public computer.

What is Session ?
Same as cookie but the information will store on the website server. It has only a unique
identifier is stored on the client side (called a "session id"). This session id is passed to the
server every time the internet browser sends an HTTP request. The web app pairs this session
id with internal database and retrieves the stored information for user by the requested page.

What is the difference between Cookie and Session ?


Cookies and session both store info about the user but the difference is that cookies store info
on local computer - internet browser (we called is client-sided) and sessions store info on the
cloud computer - website server (we called is server-sided). Cookie is limited in the storage
and only stores limited content for each user while Session is not limit in such a way. You can
delete a cookie manually by pressing Delete Cookies in the Settings menu of internet browser
You also end a session by logging out of a website or pressing Delete Cookies - it also end
session too

Nguyen Hoang Phuc


ITITIU11008

How to use Cookie ?

How to use Session ?

// index.jsp

// index.jsp

<html>
<body>
<form action="Main.jsp" method="GET">
First Name: <input type="text"
name="first_name">
<br />
Last Name: <input type="text"
name="last_name" />
<input type="submit" value="Submit" />
</form>
</body>
</html>

<html>
<body>
<form method=post
action="SaveName.jsp">
What's your name? <input type=text
name=username>
<input type=submit value=Submit>
</form>
</body>
</html>

Main.jsp shows how to retrieve the saved


name.
<%
Cookie FullName = new Cookie("full_name",
request.getParameter("full_name"));
Cookie IDs = new Cookie("id",
request.getParameter("id"));
FullName.setMaxAge(60*60*24);
IDs.setMaxAge(60*60*24);
response.addCookie( FullName );
response.addCookie( IDs ); %>
<html><head>
<title>Cookies</title>
</head><body>
<ul>
<li><p><b>First Name:</b>
<%= request.getParameter("full_name")%>
</p></li>
<li><p><b>Last Name:</b>
<%= request.getParameter("id")%>
</p></li>
</ul>
</body></html>

The target of the form is "SaveName.jsp"


which saves the user name in the session
<%
String name = request.getParameter(
"username" );
session.setAttribute( "theName", name );
%>
<html>
<body>
<a href="Result.jsp">Continue</A>
</body>
</html>
Result.jsp shows how to retrieve the saved
name.
<html>
<body>
Hello, <%= session.getAttribute( "theName" )
%>
</body>
</html>

You might also like