Nearby lessons
18 of 34Servlet - Session Management (Cookies)
- Understand Session Management by using Cookies
- Understand Demo Program for session Management by using Cookies
- Understand Demo Program how cookies are exchanging b/w client and server
- See complete working code examples
Session Management (Cookies) is an essential part of the Java Servlet technology. This lesson explains Session Management by using Cookies, Demo Program for session Management by using Cookies and CookieDemoServlet1.java with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid.
Session Management by using Cookies
Cookie is a small amount of information(key-value pair),which is created by server and maintained
by client.
Whenever browser sends first request,if server wants to remember client information for the
future purpose, then server will creates Cookie object with the required information and sends to
the browser as the part of response.
Browser stores that cookie in the local file system and sends to the server with every consecutive
request.By accessing that cookie server can able to remember client information.
Server will use setCookie response header to send cookies to the client. Browser will use cookie
request header to send cookies to the server.
Hence by using setCookie response header and cookie request header cookies are exchanging b/w
client and server. It is exactly same as exchanging sessionid b/w client and server.
req 1
resp 1 + setCookie=C1
req 2 + Cookie=C1
resp 2 + setCookie=C2
req 2 + Cookie=C1 + C2
| Client | Server |
|---|
We can create a Cookie object by using Cookie class Constructor.
Cookie c = new Cookie(String name,String value);
Eg:
Cookie c = new Cookie("durga","10");
After creating Cookie object,we have to add that object to the response by using addCookie()
method.
resp.addCookie(c);
At server side we can retrieve cookies send by the client from request object by using getCookies()
method.
Cookie[] c = req.getCookies();
If the request does not associated with any cookies then this method returns null.
Important methods of Cookie class:
Session Management by using Cookies
returns the name of the Cookie
Session Management by using Cookies
returns the value of the Cookie
Session Management by using Cookies
Returns the max age of the Cookie in seconds.
Session Management by using Cookies
To set max age of the cookie.
setting max age as -1,then cookies will be expired automatically whenever browser window
closed.
-1 is the default value.
Demo Program for session Management by using Cookies
cookie.html:
Demo Program for session Management by using Cookies
CookieDemoServlet1.java
CookieDemoServlet2.java
session3
|-cookie.html
|-WEB-INF
|-classes
|-CookieDemoServlet1.class
|-CookieDemoServlet2.class
Demo Program how cookies are exchanging b/w client and server
cookie.html:
Demo Program how cookies are exchanging b/w client and server
CookieDemoServlet1.java
Demo Program how cookies are exchanging b/w client and server
RequestHeaderDemoServlet.java
session4
|-cookie.html
|-WEB-INF
|-classes
|-CookieDemoServlet1.class
|-RequestHeaderDemoServlet.class
Persistant cookies vs non-persistant cookies:
If we are setting max age to the cookie,then such type of cookies are called persistant cookies or
permanent cookies. These will be stored in the local file system of the client.
If we are not setting max age ,then such type of cookies are called temporary cookies or non-
persistant cookies.These cookies will be stored in the browser's cache and not visible in the local
file system. Once we close the browser, automatically these cookies will be expired.
Advantages of Cookies:
- Very easy to implement
- Persist across server restarts also
- All browsers and servers provide automatic support for cookies.
Disadvantages of Cookies:
- Cookies can be enabled or disabled at client side to meet security constraints.
If the cookies are disabled then session management by using cookies is not possible.
2.The number of cookies supported by any browser is always fixed.
- The max size of the cookie is also fixed. Hence we can not store huge amount of information by
using Cookies.
- Cookie data is always String type.
Differences b/w Session API and Cookies:
| Session API | Cookies |
|---|---|
| 1) Session Information will be maintained at | 1) Session Information will be maintained at |
| Server side. | Client side. |
| 2) Best suitable if we want to store huge | 2) Best suitable if we want to store less amount |
| amount of Information. | of Information. |
RequestHeaderDemoServlet.java
If Cookies are disabled at Client Side then what will happend?
If the cookies are disabled at client side then browser is unable to see Set-Cookie response header.
Hence browser wont get any cookies or session id send by server.
If the cookies are disabled at client side then browser unable to send Cookie request header.Hence
server wont get any cookies or session id from the request and every request is treated as new
request. Due to this total session management fails.
To overcome this problem,we should go for the most powerful and painful technique: URL
REWRITING.
Session Management by URL REWRITING
URLs can be re written or encoded to include session information.This technique is called url
rewriting.
URL Rewriting=URL+Session Info
Eg: url;JSESSIONID=1234
HttpServletResponse defines the following methods to append session id to the url.
RequestHeaderDemoServlet.java
Returns url by appending JSESSIONID.
RequestHeaderDemoServlet.java
Returns url by appending session id.
This can be used as argument to sendRedirect() method.
The above 2 methods will append JSESSIONID to the url iff cookies are disabled at client side.
If the cookies are enabled,these methods return the same url without appending JSESSIONID.
At server side we can identify whether sessionid is coming as the part of url or from the Cookie
request header by using the following methods of HttpServletRequest.
RequestHeaderDemoServlet.java
By using these methods we can identify underlying session management technique.
- Key ideas of Servlet - Session Management (Cookies) explained simply
- Ready-to-use code examples
- Exam-style questions at the end