Solved

my cookie is not getting

Posted on 2004-04-29
19
310 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:mayankeagle
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:mayankeagle
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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:mayankeagle
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:mayankeagle
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:mayankeagle
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:mayankeagle
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

705 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

18 Experts available now in Live!

Get 1:1 Help Now