Unit 3
Unit 3
Unit 3
What is Cookie?
Small text files send by the browser
Used to solve Request/Response paradigm.
Cookie contains:
Cookie Name
IP of the web server where the cookie originated
Unique ID number
A date and timestamp
Any other pertinent text based information
Types of Cookies
}
}
Deleting Cookie
Cookies
URLs Rewriting
Hidden Variables
Session
Cookies
Three steps to creating a new cookie (simple):
1) Create a new Cookie Object
Cookie cookie = new Cookie (name, value);
2) Set any cookie attributes
Cookie.setMaxAge (60);
3) Add your cookie to the response object:
response.addCookie (cookie)
Disadvantages
cookies can be deleted / disables by client
URL Rewriting
Client appends some extra data on the end of each URL that identifies the
session
Server associates that identifier with data it has stored about that session
Advantage
Works even if cookies are disabled or unsupported
Disadvantages
Has a lot of tedious work to do processing to do
Must encode all URLs that refer to your own site
Allpages must be dynamically generated (no static HTML pages)
because you need to add user data to url
For eg : response.sendRedirect(“Welcome?name=Neelam”);
Hidden Fields
It is a textbox whose visible attribute is set to hidden & whose enterable
attribute is set to off.
Works only when an HTML form is submitted to web server for further
processing.
<INPUT TYPE="HIDDEN" NAME="session" VALUE="...">
Advantage
Works even if cookies are disabled or unsupported
Disadvantages
Lots of tedious processing
All pages must be the result of form submissions
Session
Servlets include a built-in Session API:
Enables you to very easily create applications that depend on
individual user data
For example:
Personalization Services
Maintaining state about the user’s preferences.
Servlet API Basics
For example:
HttpSession session = request.getSession(false);
Hash Map that enables you to store any type of Java object.
You can therefore store any number of keys and their associated
values.
Example:
Integer accessCount =
(Integer)session.getAttribute("accessCount");
Example:
session.setAttribute("accessCount", accessCount);
key Value