Cookies
Cookies
Cookies: Cookies are information that a Web server sends to a browser and that the browser returns back to web server when visiting the same Web site later. Cookie can be saved in a browser as a key value pair To add a cookie in browser we need to create an object for cookie class and use a HttpResponse object to send the data to browser. To create cookie object: Cookie ck=new Cookie(key,value) To add cookie to browser response.addCookie(ck); // addCookie method takes cookie objects as argument
To get or retrieve the all the cookie present in the browser we need to use a HttpRequest object To get cookie: Cookie ck[]=request.getCookies();
To display cookie present in cookie array: for(int i=0;i<ck.length;i++) {String str=ck[i].getName(); String value=ck[i].getValue(); out.println("<b><br>Name of the Cookie :"+str); out.println("<b><br> Value of the cookie :"+value); }