Solved

my cookie is not getting

Posted on 2004-04-29
19
323 Views
Last Modified: 2010-03-31
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.
0
Comment
Question by:rosshind
  • 6
  • 5
  • 3
  • +2
19 Comments
 

Author Comment

by:rosshind
ID: 10950859
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!
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10950909
hello,

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

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

R.K
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10952812
>> First person to say hello gets 500 points!
Why ? Just delete the question : you answered the question yourself, so you won the points.
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10952817
You can ask to delete the question here : http://www.experts-exchange.com/Community_Support/askQuestion.jsp
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10955619
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
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10957702
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.
0
 

Author Comment

by:rosshind
ID: 10957971
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..
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10958046
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10958294
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



0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10958324
>> 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.
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10958492
thats authors wish...y you bother that..much...i dont; understand...????? as if u r issuing the points........

R.K
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10958554
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.
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10958559
i agreeee........awaiting for Author to Comment........????????????

R.K
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10970997
Recommendation: PAQ/ Refund.
0
 

Author Comment

by:rosshind
ID: 10986727
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.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10986996
Well, its ok with me.
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 10990066
Closed, 500 points refunded.

modulo
Community Support Moderator
Experts Exchange
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now