4
Cookie Cookie is a piece of information with name and value which will be stored in the client machine. cookies will be created in server machine and will be moved to client machine along with response object . by default container create one cookie for the session management. Its name is JSessionId and its value is SessionId generated for the particular client. There is a class called Cookie in “javax.Servlet.http” package . C:\>javap javax.servlet.http.Cookie Cookie extends Object implements Cloneable { Cookie(String,String); void setComment(String); String getComment(); void setDomain(String); String getDomain(); void setMaxAge(int); int getMaxAge(); void setPath(String); String getPath(); void setSecure(boolean); boolean getSecure(); String getName(); void setValue(jString); String getValue(); int getVersion(); void setVersion(int); Object clone(); } Following are the steps what can container has to do for the session management :--------- I. Container verifies the incoming request whether it contains the cookie with the name JsessionId or not. Cookie ck[]= req.getCookies(); for(int i=0;i<ck.length;i++) { String cn=ck[i].getName(); if(cn equals(“JsessionId”)) { Proceed to process to results } else { Mark the client as a new client ; } } II. The client is new client then container will do the following:------------ Cookie Methods Note :-------- when the container generating the SessionId it will use the following things Client IP address Time stamp (time in terms of milliseconds ) Some sequence number (depend on number of the session object will be created)

Cookies

  • Upload
    phani

  • View
    212

  • Download
    0

Embed Size (px)

DESCRIPTION

Cookies

Citation preview

Cookie Cookie is a piece of information with name and value which will be stored in the client machine. cookies will be created in server machine and will be moved to client machine along with response object . by default container create one cookie for the session management. Its name is JSessionId and its value is SessionId generated for the particular client. There is a class called Cookie in java.Servlet.http! package .C:\>javap javax.servlet.http.CookieCookie etends "bject implements Cloneable{Cookie#String$String%&void setComment#String%&String getComment#%&void set'omain#String%&String get'omain#%&void set(a)ge#int%&int get(a)ge#%& void set*ath#String%&String get*ath#%&void setSecure#boolean%&boolean getSecure#%&String get+ame#%&void set,alue#jString%&String get,alue#%&int get,ersion#%&void set,ersion#int%& "bject clone#%&}-ollowing are the steps what can container has to do for the session management ./////////I. Container verifies the incoming re0uest whether it contains the cookie with the name JsessionId or not.Cookie ck123 re0.getCookies#%&for#int i34&i5ck.length&i66%7String cn3ck1i2.get+ame#%&if#cn e0uals#JsessionId!%%7*roceed to process to results8else7(ark the client as a new client &88II. The client is new client then container will do the following.////////////a) Generate the sessionId.b) Container creates HttpSession bject Cookie!ethods"ote .//////// when the container generating the SessionId it will use the followingthings Client I* address Time stamp #time in terms of milliseconds %Some se0uence number #depend on number of the session object will be created%9y clubbing all these value $ container generates one big number and converted to the headecimal number. The result is uni0ue SessionId."ote ./////// container creates the objectof subclass of :ttpSession interface .which is provided by web container vendor .String sid3 ///////////:ttpSession session3new :ttpSession Impi#%&;;assume this class has given by web container vendor String sid3 ///////////:ttpSession session3new :ttpSession Impi#sid%&;;assume this class has given by web container vendor c% Container creates the cookie with the name JsessionId .Cookie ck3new Cookie#JsessionId!$sid%&d% Container add the cookie to response object.res.addCookie#ck%&in the above scenario container is using cookie to carry the SessionId from server to client or from client to server .we can also use cookies to store client conversation State .following scenario show how to use cookies to store the conversation State of the client .///////you can use cookies in two ways as discussed above ./////cookies are always not safe because of the following reasons .///////// browser may or may not support cookies cookies may be deleted by mistakebecause those are storing in the client machine .so to store conversation State use :ttpSession object always Container is using cookies for the default session management if you don?>ewritting $:idden -ields =>?>ewriting .////////when you send the @st re0uest $ container create one cookie with the name JsessionId and send it to the clientmachine .if your browser not supporting your cookies then net re0uest going to the server will go without SessionId.then container treats re0uest as @st re0uestand client has new client and generates another SessionId and create another Session object.9ecause of this we can?. Ho#e.jsp$ddServletSho%ServletSho%.jsp 9ook@9ookB9ookC9ookDShow booksbn3reg.get*arameter#bname!%&Cookie ck3new Cookie#bn$bn%&>es.addCookie#ck%&)rray?ist al3new )rray?ist#%&Cookie c123re0.getCookies#%&-or#int i34&i5#c.length#%&i66%7String cn3c1i2.get+ame#%If#cn e0uals#JsessionId!%7)l.add#cn%&88res.set)ttribute#books!$al%&)'')'')'')''////////////////&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& ?ogin Servlet 'o(in.ht#l :ome.jsp Search.jsp Search )es*lt =sername *assword ?ogin:ellousername//////////////////////////////////////////)dd Student=pdate studentSearch studentEnter your Sid SearchFE?C"(E T">ES=?T/////////////////////////////// //////////////// GGHHIn Home.Jsp.///////////////////////5a href3!)dd.jsp!I)dd Student 5;aI5a href3!=pdate.jsp!I=pdate Student 5;aI5a href3!5J3res.encode=rl#add.jsp!%JI !I)dd Student 5;aIIn Search.jsp.///////////////////////////////////5formI +nter Sid 5brI5input tyoe3!tet! name3!sid!;I5brI5input type3!hidden! name3!JSESSI"+I'! value3!5J3session.getId#%JI;I5input tupe3!submit! value3!SE)>C:!I5;formI