Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in cookie

 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello i need to store number of visitors in cookie, and also if number of visitors are even than i have to show the names, and if odd than i have to show the nicknames,
i m doing like this:



here it's giving me an erorr, at line
Cookie cookieVisitors = new Cookie("visitors", hitCount);

it's because Cookie takes two String type arguments and here i need to pass counter as a integer..
how can i do this??
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit Jain wrote:hello i need to store number of visitors in cookie

No you don't. Cookies are specific to one specific browser for one specific user. That means that if I visit your site in Firefox, that cookie may have a different value from when I visit your site in Chrome. If you then visit your own site in Chrome you may get a different value again. If the visits are only stored in cookies then my Chrome browsing session isn't aware that I already visited your site with Firefox, and it's definitely not aware of your visits.

Number of visitors are a measurement that belongs to the server, not the client. That means you must use server side techniques for storing them, and that rules out cookies. In memory you can use servlet context attributes, for persistence you need to use a database, a physical file or something else like that.
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya that i know, but this is just like my assignment, and i m just doing this but i m not going to implement it anywhere??
so i only need to test here, how to store visitors into cookies, and write now i am running my website on local..
so this is only mt learning activity..
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you basically only need to convert an int to a String? Check out some of the static methods of Integer and String, there are a few that can help you out.
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:So you basically only need to convert an int to a String? Check out some of the static methods of Integer and String, there are a few that can help you out.


okay it's working for me now, thank you...
but one problem still here is at the first time it showing me Name and the second refresh, nickName, but after that my if else is not working i mean counter are increasing after each refresh but always showing nickName..

here is my updated code:

 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try hitCount%2
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
upps, silly mistake, working now....
Thanks...
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one more thing i want to know here,as i initialized variable count in class

int hitCount=0;


so is it thread-safe ??
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Instance variables are not thread-safe. All the threads share the same instance.

Punit Jain wrote:one more thing i want to know here,as i initialized variable count in class

int hitCount=0;


so is it thread-safe ??
 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit Jain wrote:one more thing i want to know here,as i initialized variable count in class

int hitCount=0;


so is it thread-safe ??

It will be when you move that line into the doPost method.
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Koen Aerts wrote:
Punit Jain wrote:one more thing i want to know here,as i initialized variable count in class

int hitCount=0;


so is it thread-safe ??

It will be when you move that line into the doPost method.


yes than it will be, but if i move the line into doPost, than it will not increment my counter, (ie. it will initialized each time)..?
 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit Jain wrote:
Koen Aerts wrote:
Punit Jain wrote:one more thing i want to know here,as i initialized variable count in class

int hitCount=0;


so is it thread-safe ??

It will be when you move that line into the doPost method.


yes than it will be, but if i move the line into doPost, than it will not increment my counter, (ie. it will initialized each time)..?

The counter's initial value should come from the cookie. If there is no value in the cookie (i.e. first visit, or user deleted or blocked cookies) then it's initial value can be set to 0. Then you increment it and update the cookie with the new value.
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay....
 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, aggregate information such as "Number Of visitors using cookies is" and "You are Visitor number" cannot be implemented by simply and only using cookies. This has been explained already in your how to count number of visitors thread. You have to keep track of such information on the server side.
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yap i know, i m doing this only for my learning exercise..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic