Hello i am trying to use cookie at my site. I used this code for set the cookie

  try{
       Cookie cookie1 = new Cookie("thesiteid", Integer.toString(site_id));
       cookie1.setDomain("localhost");
       cookie1.setPath("/");
       cookie1.setMaxAge(24*60*60*365);
       response.addCookie(cookie1); 
       System.err.println("Set cookie");
   }
   catch (Exception e){
       System.err.println("Error in creating the cookie: "+e.getMessage());
   }

The problem is that is not working at chrome and i am not sure if it does not work to other versions of IE. How i can make a more universal create cookie?
Thank you very much

Seems there is no issue with this code.
Have you checked if cookies are not disabled in your browser?

And how are you checking this cookie on your web page?
can you put here that piece of code that shows how you are testing this cookie in the jsp? or in any other servlet?

i have checked that the cookies are not desabled. It is working at mozilla,safari,ie but not chrome for some reason.
To check the if the cookie exists i use this code

Cookie[] cookies = request.getCookies();
    for(int i = 0; i < cookies.length; i++) { 
            Cookie cookie1 = cookies[i];
            if (cookie1.getName().equals("thesiteid")) {
               // System.err.println("thesiteid = " + cookie1.getValue());
                cookiesiteid = Integer.parseInt(cookie1.getValue());
                foundCookie = true;
            }
    }

Something is wrong and does not work at chrome. Maybe the command cookie1.setDomain("localhost"); has a problem and not work at chrome

yes you are right.
Some browsers sometimes reject cookies served from domain rather than the page itself.
So if you want to manage cookies on the page then use javascript instead.
You can find an useful article here http://www.quirksmode.org/js/cookies.html

See, cookies are dependant on browser setting if browser is enabled to accept cookie then
the cookie mechanism works but if he /she disabled it then no matter how hard u try the mechanism will not work.So,to solve your problem you have to check wheather in your chrome bowser setting allows to accept cookie or not.

i just want to ask, if an antivirus can also affects this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.