Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Retrieve cookie values

 
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.
 
Marshal
Posts: 67424
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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());
}
}


}
 
Bartender
Posts: 2856
10
Firefox Browser Fedora 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.
 
    Bookmark Topic Watch Topic
  • New Topic