Thursday, May 30, 2013

Reading cookies from browser using core java

Cookie:

A cookie is a piece of information sent by server(web program) to client(browser).

A cookie will have a name and a value which will be stored in browser's cache.
There are two types of cookies available.

--> Persistent cookies and 
--> Non Persistent cookies

Persistent cookies will be stored in browser's cache even though we have closed the browser until some time specified in the web program in which they got created.

Non Persistent cookies will be removed from the browser's cache whenever we close the browser.

In JSE edition, java.net package provides the feature of reading cookies from browser.

This can be illustrated as shown.

CookieManager will manages all the cookie related tasks and will be helpful in getting cookies.

As part of CookieManager interface we have some final static fields available to set the cookie policy.

Content policy describes the acceptance state of cookies. i.e, which cookies to be accepted and which should be rejected.

The cookie policy has predefined policies namely ACCEPT_ALL, ACCEPT_NONE and ACCEPT_ORIGINAL_SERVER

ACCEPT_ALL is to accept all cookies , ACCEPT_NONE  is to reject all cookies and ACCEPT_ORIGINAL_SERVER is to accept cookies from original server.




A sample program to read the cookies stored in browser with java net package is as shown.


Once if you run the application, CookieManager will set the cookie policy to ACCEPT_ALL so that it can accept any cookie. A CookieHandler is used by the http protocol and can be registered by using setDefault().

Once we have opened the connection to the specified URL by using CookieManager we can get the CookieStore which contains a bunch of cookies related to the url. 

By using CookieStore we can get the list of cookies with their name and values

The output of the application will display list of cookies in the browser related to the url with cookie name and value.



12 comments:

  1. Nice core java guide..keep it up.

    ReplyDelete
  2. nice doc bro. Appriciate your time can you tell me how to capture a cookie when I login to a server the cookie is immediately droping. i have to capture that.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. didn't get your question. Just to inform you, Cookie will be created in server related classes by us and stores in browser cache when first request goes to server page. As per my understanding of your question if you want to use cookie further go with session. Set the cookie to session instance and use it further.

    ReplyDelete
  5. I have read your blog its very attractive and impressive,Thanks for sharing

    Java Online Training

    ReplyDelete
  6. Interpersonal communication has always been a basic necessity of a human being. To facilitate this requirement, many mediums of entertainment have been doing well. Some of them are Telephone, mobiles and speed posts. All of them have distinct characteristics and have carved a niche in a competitive market. Among all of them, the new to hit the circuit is the facility of free PC to free phonecalls

    ReplyDelete
  7. So below I have made a list of 5 of the Best Paid Android Apps. Many Android apps have both paid and free versions, the apps I talk about below are worth purchasing for the extra features. check over here

    ReplyDelete
  8. Electric Broadband is an innovation in the recent technology trends. This technology is certainly encouraging and infrastructure cost effective model to offer broadband at high speed internet access - having penetration even into the rural areas since every home in the world is served by power lines. phone tracker

    ReplyDelete
  9. hi!,I like your writing so a lot! share we communicate far more about your article on AOL? I need a specialist on this area to solve my problem. May be that’s you! Looking forward to see you. Mega888 apk download

    ReplyDelete
  10. Electric Broadband is an innovation in the recent technology trends. This technology is certainly encouraging and infrastructure cost effective model to offer broadband at high speed internet access - having penetration even into the rural areas since every home in the world is served by power lines. spy camera app

    ReplyDelete
  11. As I website owner I think the articles here is rattling superb , thanks for your efforts. 스포츠토토

    ReplyDelete