Cookies

You might also like

Download as pdf
Download as pdf
You are on page 1of 4
Cookies [83 Where Cookies are used? “Cookies can be used by web servers to identity and track users as they navigate different pages on a website, and to identify users returning to a website, The cookies can be used for following purposes : @ = torecognise your computer when you visit the website. @ to track you as you navigate the website, and to enable the use of any e-commerce facilities. © to improve the website's usability. @ toanalyse the use of the website, © inthe administration of the website. © to personalise the website for you, including targeting advertisements which may be of particular interest to you, @ to build a profile of your web surfing. Secure websites use cookies to validate a user's identity as they browse from page to page; without cookies, login credentials would have to be entered between before every product added to cart or. wish list. Cookies enable and improve : @ Customer log-in @ Persistent shopping carts © Wish lists @ Product recommendations @ Custom user interfaces (i.e. "Welcome back, Steve") © Retaining customer address and payment information (only up to permitted.) Where are Cookies Placed? Cookies are maintained by your web browser, so the method for viewing them will vary depending on which browser you are using. The method for how to view cookies is listed below : © Microsoft Internet Explorer 8.0 - 11.0 : Select the 'Tools->Internet Options’ menu item, and then click the 'Settings' button under the ‘General’ tab. Click the 'View Files' button to see a list of the temporary internet files that Explorer has stored. Cookies have names that begin ‘cookie: - the Internet Address column will tell you which website created the cookie. Note : the 'Tools' option can also be accessed by clicking the ‘gear’ Explorer's toolbar. @ Microsoft Edge : Microsoft Edge does not appear to provide an option to view individual cookies, You can delete all cookies, or enable/disable them. © Firefox 7.0 and newer : Select the 'Tools->Options' menu item (may be under the Firefox button) and click on the Privacy’ tab. Click the ‘remove individual cookies! link, and you will be shown a list of all cookies stored by Firefox. You can delete selected cookies if you wish, or you can click 'Remove Ail Cookies’ to delete them all, or simply browse through the list to see which websites have stored cookies on your computer. Google Chrome : Choose 'Settings' on the ‘parallel bars' button (or the ‘Customize and control' menu), and click the ‘Advanced! link at the bottom of the screen. In the ‘Privacy and Security’ section, click on the ‘Content settings...’ button then click the ‘Cookies' option, followed by ‘See all cookies and site data’, You will be shown a list of all websites which have stored cookies on your computer. icon in Internet Eni law TXB AT) Sy and selecting a cookie Shoy, i the cookies, Selecting a website allows you to see 7 you the dota stored in it the 'Setings' or ‘pit men Noy : 7 Fences...’ on s : d clic ai : ” arte aan ere ler cp the vay tb and lk th Pin tahoe ‘wil be ahown a tale ll cookie Sing None st Individual cookies can be deleted by selecting them Canal be deleted by clicking Remove All manent $$ & : : n " ania kes working, with coop, ” Serviet API provides javax.servlet.http.Cookie which make: ie very easy. 8.4.1 Working with Cookies 1. Creating a Cookie Constructor To creates a cookie Cookie( ) constructor is used, a small amount of information sen, 8 servlet to a Web Set, saved by the browser, and later sent back to the Server. 4 Cooke's value can “nlauely identify a client, s0 cookies are commonly used for Se8sion management A cookie has a name, a single value, and optional attributes such as . comment, path and domain qualifiers, a maximum age, and a version number. Some Web browsers have bugs in how they han le the optional attributes, so use them sparingly to improve the interoperability of your servlets. The servlet sends Cookies’ “to! ‘the, browser by usin, _ the to HTTP erPonseaddCookicjvan seven Coat e method, which adds fields to HTTP response teaders to send cookies to the ‘Ser, one at a time. The browser is cooprted to support 20 cookies fox each Web server, 300 cookies total, and may limit Cookie size to 4 KB each, Syntax: public Cookie(javalang String ame, java.lang.String value) Iteonstructs a cookie with a Specified name and value. ‘Some important methods of Cookie class are 1. getComment{ mument describing the purpose of this cookie, used at client side, 2 getDomain( ) : Returns the domain name fo, method to i Pao okie will be sent to specific renounce se setPath( )to instruct browser W2 send cookie to a particular Tesource only, 6 getSecure( ) ; Returns true if the browser is Sending cookies only over a secure Sete oF false if the browser eat endl cookies using any protocol, We can wr procete( ) method to insmace browser to send cookie only over secured Protocol, 7. * Reale) :retums the value of the cookie ‘method to change the Cookies re 8. getVersion( ) : Ret . give aay Ratu the version of the protocol this cookie complies with ; for version 9. isHttpOnly() ; ie a ne this Cookie has been marked as HttpOniy. There _ i colgudeea oe € can use to instruct client to use it for HTTP only You can wri na Haire using the HttpServletResponse object like this ie = new Cookie(‘myCookie’, "myCookieValue’) response.addCookie(cookie) As yt you can see, the cookie is identified by a name, *myCookie", and has a value *myCookieValue’. Thus, you i (names) I's bit like a Hashtable, eae eee Whenever the brow: oe sacle eeleiderree the web application it submits the cookies stored on Oe deci a web application. Only cookies stored by the accessed web pplication mitted. Cookies from other web applications are not ena IIL. Reading Cookies Sent From the Browser You can read the cookies via the HttpServletRequest like this: Cookie[ ] cookies = request.getCookies( ); Note : the getCookies( ) method may return null! Now you can iterate through the arra i : yy of cookies and find the cookies you need. Unrate ie is no way to obtain a cookie with a specific name. The only way t0 fod at on gain is to iterate the Cookie| ] array and check each cookie name. Here is Cookie[ ] cookies = request. getCookies( ); String userld = null; for(Cookie cookie : cookies){ if("uid" equals(cookie.getName( ))){ userId = cookie.getValue( ); 1 This example finds the cookie with the name uid’ and stores its value in Cookie{ ] array. If you need to access more than one and put the Cookie instances into a Map, instance as value. Here is how that could look : Map cookieMap = new HashMap( ); Cookief] cookies = request.getCookies( ); terate the Cookiel ] array once, wy, and the Cookie cookie, you could i tusing the cookie name as key for(Cookie cookie: cookies){ cookieMap-put(cookie.getName( ), cookie); ‘After this code is executed, you can Tow access the cookies in the cookieMap using the cookie names as keys (cookieMap.get("cookieName’)) @t¥ne.1t tedelen ee) XY Enterprise Java (T-Y.B.Sc-I.T,) en) 68 wee IV. Cookie Expiration 0) n time, This time tells ti One important Cookie setting is the cookie voi okie before deleting n browser receiving the cookie how long time it should keep method. This method ta You set the cookie expiration time via the setMaxAget } is an example : Ms the number of seconds the cookie is to live as parameter. le Cookie cookie = new Cookie( "uid", "123"); cookie.setMaxAge(24 * 60 * 60); // 24 hours. Tesponse.addCookie(cookie); This example first creates a Cookie instance with the name "uid" a the value "123 Second, it sets the expiration to 24 hours using the setMaxAge( ) method, 24 hours is 60 seconds x 60 minutes x 24 hours (24 x 60 x 60). Finally the example sets the cookie on the HttpServletResponse object, so the cookie is included in the Tesponse sent to the browser, V. Removing Cookies : Sometimes you may want to remove a cookie from the browser. You do so by setting cookie expiration time. You can set the expiration time to 0 or -1. If you set the expiration time to 0 the cookie will be removed immediately from the browser. If you set the expiration time to -1 the Cookie will be deleted when the browser shuts down. Here is an example : Cookie cookie = new Cookie("uid", "); cookie.setMaxAge(0); Tesponse.addCookie(cookie); ly has a cookie stored with the name " h the same name ("uid") ly have the cookie stored, Piration time is 0, ‘uid", it will be deleted after with an expiration time of 0. If the this new cookie is just thrown out QUESTIONS What are Cookies? Explain its parameters, Write short note on types of Cookies, Explain the uses of Cookies, Where are Cookies placed in computer? Explain in brief the creation of Cookie. List and Explain important methods of Cookies class, Explain setting, sending and reading of Cookie Write short note on Cookie expiration and rem euNuaueone in java servlet. oval. = ea

You might also like