Link to home
Start Free TrialLog in
Avatar of rosshind
rosshind

asked on

my cookie is not getting

My cookie is not getting.  I use the below code to set a cookie the first time the page is requested then to get it on every subsequent request:

 String strAnswer= getCookie("UserCookieID", request);
   if (strNewUserCookieID != null){
   setCookie("UserCookieID", strNewUserCookieID, response);
   }


I'm confident my cookie is being set (because I can see it in the browsers cookie manager), the problem is that my getCookie function is always returning null.  Here are teh functions I use for getting and setting:

   <%!
   
    public void setCookie(String strName, String strValue, HttpServletResponse response)
    {
   
Cookie cooCookie = new Cookie(strName,strValue);
cooCookie.setMaxAge(50*365*24*60*60);
cooCookie.setPath("/");
response.addCookie(cooCookie);
rmhUtil.writeDebug(new Exception(),"cookie has been set:"+ cooCookie.getValue());

}
   %>
   
   <%!
   public String getCookie(String strName, HttpServletRequest request){
   
   String strAnswer=null;
   
   Cookie[] cookies = request.getCookies();
if (cookies != null)
{
  for (int i = 0; i < cookies.length; i++)
  {
     String name = cookies[i].getName();
     if (name==strName){
     strAnswer=cookies[i].getValue();
     }
   
  }
}

return strAnswer;

}
 %>  


can you please explain why my cookie is not 'getting' please??  - 500 points if you can.
Avatar of rosshind
rosshind

ASKER

Its ok, I just realised the problem

I should have been using

 if (cookies[i].getName().equals(strName)){
     strAnswer=cookies[i].getValue();
     }

instead of == to compare the strings

First person to say hello gets 500 points!
hello,

exactly you can use only...
.equals();

cookies[i].getName().equals(strName)...

R.K
>> First person to say hello gets 500 points!
Why ? Just delete the question : you answered the question yourself, so you won the points.
hello Webstorm,

 if he wants to give away the points , what is your difficulty...why unnecessarly spoiling the getting points,
he wants to give he is giving..then whats the problem...for you...

if u r the 1st person to say "hello"...what u will do..? i think its his decission, so leave him on his decission...

R.K
Avatar of Mayank S
Ram Krishna,

I would like to support Webstorm.

EE is supposed to be site where professionals and experts interact and exchange their knowledge and information. Its not merely an issue of getting the maximum points. You have probably not read the member agreement completely.

This page, after being PAQed, will be a resource and will be displayed in search results to other people who come looking to this site for solutions. They might not go through the whole page but might just click on the "See Solution" link, which will lead them to your comment - which was not the correct solution. The solution was found by the questioner himself.

rosshind,

I have always kept you in my list of good questioners (you can see that on my profile), and I hope you know that accepting comments which were not correct answers is against the member agreement. If you find the solution to a problem yourself, you should get the question deleted - a better way would be to get it PAQed with points refunded, in which case a moderator will close this question by refunding your points but it will not be deleted and it will be available in the knowledge-base for others to visit and see the correct solution.

Just a thought for your question's technical scope - you should also trim () the values - sometimes errors arise becasue of that (but of course, this depends on the scenario). You could also try equalsIgnoreCase () instead of equals ().

Mayank.
Ok,

I didnt realise it was such an issue.  When I have some time I'll sort it out.  It would be a lot easier if there was an Accept button for my own question..
Well, you can't give points to yourself ;-) otherwise everybody could give too many points to themselves and be at the top ;-)

You should post a 0-point question in Community Support, give a link to this question and ask them to PAQ it, with points refunded. A moderator will close it (generally by accepting his own comment as an answer) and your points will be refunded. The question will stay in the knowledge-base for others to see if they come searching upon the same topic.
Hi mayankeagle,

I will strongly appreciate your statements and comments.....
but..before that..

in u r statement u mentioned that..

>>> which will lead them to your comment - which was not the correct solution.
how can u say this statement,  please look at the answer first before you give any statement, i have provided the answer already... and if you talk about the Author comments, i don't care about what Author commented...

Finally i will leave this to the Author...????????

Thanks a lot....once again..
and best of luck..

R.K



>> i have provided the answer already

No, you have not. You have repeated what the author said. He was himself the first person to post the solution.

>> i don't care about what Author commented

You have to ;-) because its his question. If he says that he has solved it himself, then it means that he has solved it. You can't falsify that by saying that you "don't care what the author says" and still try to repeat the answer by saying the same thing.
thats authors wish...y you bother that..much...i dont; understand...????? as if u r issuing the points........

R.K
Rama Krishna,

The author's wish is not everything. In fact, he has also realized what is correct now. If you are a good member and have read the member agreement, then you would know what is correct and what is not. This is not the only question on EE. You will have many more to answer and take the points.
i agreeee........awaiting for Author to Comment........????????????

R.K
Recommendation: PAQ/ Refund.
Look I'm really sorry for this, I dont want to upset anyone.  I have requested the points to be refunded back to me.

Sorry about the mix up.
Well, its ok with me.
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial