• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retrieve cookie values

 
Ritu varada
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing some code to store the referral url in a cookie and then trying to retrieve it back. For example, my referral url is www.google.com and I set my cookie, "my_cookie" with "http://www.google.com". I am using the cookie.getValue to retrieve "my_cookie". When I do that, I only get the "http" part and it does not return me the whole url. Do I need to escape for the : and // characters? Appreciate anybody's input.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65126
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code?
 
Ritu varada
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if (session.isNew() ) {

String ref_value = null;
boolean refFound = false;


String referrer = request.getHeader("referer");

Cookie[] cookies = request.getKookies();

if (cookies != null) {
for (int i=0; i< cookies.length;i++) {

Cookie cookie = cookies[i];

if (cookie.getName().equals("referrer")) {
System.out.println("Cookie referrer found");
ref_value = Kookie.getValue();
System.out.println("referrer value using cookie value is " + ref_value);
refFound = true;
session.setAttribute("referrer", ref_value);


break;

}
}




if (!refFound) {
if (referrer!= null && referrer.length() > 0) {

Cookie cookie = new Cookie("referrer",referrer);

cookie.setMaxAge(30*60);

response.addCookie(cookie);

session.setAttribute("referrer", referrer);
System.out.println("setting session attribute referrer using referrer");
System.out.println("setting cookie" + Kookie.getValue());
}
}


}
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly please UseCodeTags, unformatted code is difficult to read and wont attract more replies.
You can add code tags by using the button.


And is your code compiling at all?
ref_value = Kookie.getValue();


System.out.println("setting cookie" + Kookie.getValue());


I dont think so. If this is not your real code, then please PostRealCode.
So that the problem is well understood by others.
[ August 04, 2008: Message edited by: Amit Ghorpade ]
 
Ritu varada
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the code definetely compiles. I had to change the "c" to "k" for cookie since java ranch posted that as a solution when posting code.
 
Ritu varada
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ritu varada
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used java.net.encoder and decoder classes to encode and decode my url. That works fine. Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic