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.



27 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. 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
  10. Children are our wealth. They are the joy of our lives. They make us feel precious, happy, sad, angry, but we love them come what may. In a world where parents are increasingly relying on nannies to raise their children, ensuring the safety of the kids has become a very big issue. With numerous incidents of nanny abuse and accidental child deaths making to the newspapers on a regular basis, the use of nanny cams, or nanny cameras, to protect and keep your children safe, is one of the better security options. best spy cameras in the world

    ReplyDelete
  11. Thank you for sharing excellent information. Your website is very cool. I am impressed by the info that you?ve on this website. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for more articles. buy steroids

    ReplyDelete
  12. You should participate in a contest for top-of-the-line blogs on the web. I’ll recommend this web site! workout fashion

    ReplyDelete
  13. you’ve gotten an important weblog right here! would you wish to make some invite posts on my weblog? Mobile application development platform

    ReplyDelete
  14. Heya i am for the primary time here. I found this board and I find It truly useful & it helped me out a lot. I’m hoping to give one thing back and aid others such as you helped me. link building services

    ReplyDelete
  15. My favorite city out of all I’ve been to, no doubt about it that its St Gallen Switzerland. Switzerland just has this nice connotation about it. Something I absolutely love, it is peacefull and comforting. sugaring hair removal

    ReplyDelete
  16. Your blog is amazing dude, i love to visit it everyday. very nice layout and content .     토토사이트 벳이스트

    ReplyDelete
  17. Pardon me if this is off-topic, but more people need to really focus on what just happened in Oslo. Just remember it could happen in our country too. hgh for sale

    ReplyDelete
  18. You made some first rate factors there. I seemed on the internet for the difficulty and located most people will go along with together with your website. hgh for sale

    ReplyDelete
  19. Hair transfer also known as hair transplantation in turkish name sa ekimi is the operation where the hairs are transplanted to the canals which are opened on the direction of the persons former hair by lateral slits sa ekimi to the requested area. The follicles which are genetically healthy are located in the area between two ears and top side of sa ekimi the nape area are taken one by one with the help of micromotor that is used by Med Hair team. steroids for sale

    ReplyDelete
  20. The mobile web is getting more attention as portable computing and mobile devices become more popular and more affordable. No longer are mobile websites considered just for consumption on cellular phones and smart phones. best mobile app developer ins ingapore

    ReplyDelete