wood burning stoves 2.0*
The moose likes Servlets and the fly likes problem in cookie Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "problem in cookie" Watch "problem in cookie" New topic
Author

problem in cookie

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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

Joined: Aug 20, 2011
Posts: 1000
    
    2
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

Joined: Feb 07, 2012
Posts: 344

Try hitCount%2
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
upps, silly mistake, working now....
Thanks...
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
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

Joined: Nov 06, 2000
Posts: 1510
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 ??


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

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

Joined: Aug 20, 2011
Posts: 1000
    
    2
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

Joined: Feb 07, 2012
Posts: 344

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

Joined: Aug 20, 2011
Posts: 1000
    
    2
okay....
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

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

Joined: Aug 20, 2011
Posts: 1000
    
    2
yap i know, i m doing this only for my learning exercise..
 
GeeCON Prague 2014
 
subject: problem in cookie