Vous êtes sur la page 1sur 8

Advertise Us | Ask Question | forum | login |

Home

Core Java

Servlet

JSP

Struts2

Mail API

Hibernate

Spring

Android

Projects

Interview Q

Comment
<<prev

Forum
next>>

Basics of Servlet Servlet Basics of Servlet Servlet API Servlet Interface GenericServlet HttpServlet Servlet Life Cycle Steps to create a servlet in Tomcat How servlet works? Servlet with IDE servlet in Myeclipse servlet in Eclipse servlet in Netbeans ServletRequest Servlet Collaboration RequestDispacher sendRedirect ServletConfig ServletContext Attribute Session Tracking Cookies Hidden Form Field URL Rewriting HttpSession Event and Listener Filter

Cookies in Session Tracking


Cookie is a mechanism to maintain the state of an user. Before cookie, let's see what is sesion tracking and what are ways to perform session tracking.

Session Tracking
Session simply means a particular interval of time. Session Tracking is a way to maintain state of an user. Http protocol is a stateless protocol. Each time user requests to the server, server treats the request as the new request. So we need to maintain the state of an user to recognize to particular user.

Cookies in Session Tracking Session Tracking Session Tracking Techniques Cookies javax.servlet.http.Cookie class Example of using Cookies

1/7/13

Servlet Tutorial: Session Tracking - javatpoint

Filter Authentication Filter FilterConfig Useful examples ServletInputStream ServletOutputStream Annotation Servlet SingleThreadModel SSI Development Registration Example Fetching records for the given rollno Improving Performance Uploading file Downloading file Sending Email by Servlet Write data to PDF Login Example Writing Image

Why use Session Tracking?


To recognize the user.

Session Tracking Techniques


There are four techniques used in Session tracking: 1. Cookies 2. Hidden Form Field 3. URL Rewriting 4. HttpSession

1) Cookies
A cookie is a small piece of information that is persisted between the multiple client requests.A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number.

w w w .javatpoint.com/cookies-in-session-tracking

2/8

1/7/13

javax.servlet.http.Cookie class
javax.servlet.http.Cookie class provides the functionality of using cookies.

Constructor of Cookie class


Cookie(String name, String value): Constructs a cookie with a specified name and value.

Commonly used methods of Cookie class


There are given some commonly used methods of the Cookie class. 1. public void setMaxAge(int expiry):Sets the maximum age of the cookie in seconds. 2. public String getName():Returns the name of the cookie. The name cannot be changed after creation. 3. public String getValue():Returns the value of the cookie.

Other methods required for using Cookies


For adding cookie or getting the value from the cookie, we need some methods provided by other interfaces. They are: 1. public void addCookie(Cookie ck):method of HttpServletResponse interface is used to add cookie in response object. 2. public Cookie[] getCookies():method of HttpServletRequest interface is used to return all the cookies from the browser.

Advantage of Cookies
1. Simplest technique of maintaining the state. 2. Cookies are maintained at client side.

Disadvantage of Cookies
w w w .javatpoint.com/cookies-in-session-tracking 3/8

1/7/13

Servlet Tutorial: Session Tracking - javatpoint

1. It will not work if cookie is disabled from the browser. 2. Only textual information can be set in Cookie object.

Example of using Cookies


In this example, we are storing the name of the user in the cookie object and accessing it in another servlet. As we know well that session corresponds to the particular user. So if you access it from too many browsers with different values, you will get the different value.

index.html

<omato=srlt"mto=ps" fr cin"eve1 ehd"ot> Nm:ipttp=tx"nm=ueNm">b/ ae<nu ye"et ae"srae/<r> <nu tp=sbi"vle"o/ ipt ye"umt au=g"> <fr> /om
w w w .javatpoint.com/cookies-in-session-tracking 4/8

1/7/13

Servlet Tutorial: Session Tracking - javatpoint

FirstServlet.java

ipr jv.o* mot aai.; ipr jvxsrlt* mot aa.eve.; ipr jvxsrltht.; mot aa.eve.tp*

pbi casFrteve etnsHtSrlt{ ulc ls isSrlt xed tpeve pbi vi dPs(tpeveRqetrqet HtSrltepnersos) ulc od ootHtSrlteus eus, tpeveRsos epne{ ty r{ rsos.eCnetye"ethm"; epnestotnTp(tx/tl) Pitrtrot=rsos.eWie(; rnWie u epnegtrtr) Srn nrqetgtaaee(ueNm"; tig =eus.ePrmtr"srae) otpit"ecm "n; u.rn(Wloe +) Coi c=e Coi(uae,)/cetn coi ojc oke knw oke"nm"n;/raig oke bet rsos.dCoi(k;/digcoi i tersos epneadokec)/adn oke n h epne /cetn sbi bto /raig umt utn otpit"fr ato=srlt'"; u.rn(<om cin'eve2>) otpit"ipttp=sbi'vle'o>) u.rn(<nu ye'umt au=g'"; otpit"/om"; u.rn(<fr>) otcoe) u.ls(; }ac(xeto e{ytmotpitne; cthEcpin )Sse.u.rnl()} } }

SecondServlet.java

ipr jv.o* mot aai.; ipr jvxsrlt* mot aa.eve.;


w w w .javatpoint.com/cookies-in-session-tracking

1/7/13

ipr jvxsrltht.; mot aa.eve.tp* pbi casScnSrltetnsHtSrlt{ ulc ls eodeve xed tpeve

pbi vi dPs(tpeveRqetrqet HtSrltepnersos) ulc od ootHtSrlteus eus, tpeveRsos epne{ ty r{ rsos.eCnetye"ethm"; epnestotnTp(tx/tl) Pitrtrot=rsos.eWie(; rnWie u epnegtrtr) Coi c[=eus.eCois) oke k]rqetgtoke(; otpit"el "c[]gtau() u.rn(Hlo +k0.eVle); otcoe) u.ls(; }ac(xeto e{ytmotpitne; cthEcpin )Sse.u.rnl()} }

web.xml

<e-p> wbap <eve> srlt <eve-aes<srltnm> srltnm>1/eve-ae <eve-ls>isSrlt/eve-ls> srltcasFrteve<srltcas <srlt /eve> <eve-apn> srltmpig <eve-aes<srltnm> srltnm>1/eve-ae <r-atr>srlt<ulpten ulpten/eve1/r-atr> <srltmpig /eve-apn> <eve> srlt <eve-aes<srltnm> srltnm>2/eve-ae <eve-ls>eodeve<srltcas srltcasScnSrlt/eve-ls> <srlt /eve>
w w w .javatpoint.com/cookies-in-session-tracking 6/8

1/7/13

<srlt /eve>

Servlet Tutorial: Session Tracking - javatpoint

<eve-apn> srltmpig <eve-aes<srltnm> srltnm>2/eve-ae <r-atr>srlt<ulpten ulpten/eve2/r-atr> <srltmpig /eve-apn> <wbap /e-p>

download this example (developed using Myeclipse IDE) download this example (developed using Eclipse IDE) download this example (developed using Netbeans IDE)
<<prev next>>

136

0
Google +

Tw eet

27

Like

2.3k

javatpoint.com on Facebook
Like 2,377 people like javatpoint.com.

w w w .javatpoint.com/cookies-in-session-tracking

Deepak

S uren

S iv a

Rav i

Ibkar

V amsee

G aurav

P ramod

S ureshreddy

Krishna

H emant

Bhanu

7/8

1/7/13

Servlet Tutorial: Session Tracking - javatpoint


Deepak S uren S iv a Rav i Ibkar V amsee G aurav P ramod S ureshreddy Krishna H emant Bhanu

H arini

V ijay

N atasha

S achin

N itish

Jas Ram

C hera

S elv a

S hashi

Rav i

V ipul

A run

Bhoomi P rakash C handraRav i F acebook social plugin

P riy a

A shw inikumar N atasha

Raghav endra Burra Raunaq

Kaushal

H itesh

S rinu

A mit

Like the www.javatpoint.com on facebook / google+ / twitter / subscribe to get latest updates
Sitemap javatpoint.com is developed Core Java Servlet JSP Struts2 Hibernate Spring Interview Questions

to provide easy and point to point learning and training in detail. Examples might be simplified to improve reading and basic understanding. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. If there is any mistake, please mail to sonoojaiswal1987@gmail.com. We provide assurance of 90% interview questions. While using this site, you agree to have read and accepted our terms of use and privacy policy. Copyright 2011-2012. All Rights Reserved.

w w w .javatpoint.com/cookies-in-session-tracking

8/8

Vous aimerez peut-être aussi