wood burning stoves 2.0*
The moose likes Servlets and the fly likes Retrieve cookie values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Retrieve cookie values" Watch "Retrieve cookie values" New topic
Author

Retrieve cookie values

Ritu varada
Ranch Hand

Joined: Sep 08, 2004
Posts: 117
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.


SCJP,SCWCD(1.4)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61760
    
  67

Code?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ritu varada
Ranch Hand

Joined: Sep 08, 2004
Posts: 117
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

Joined: Jun 06, 2007
Posts: 2718
    
    6

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 ]

SCJP, SCWCD.
|Asking Good Questions|
Ritu varada
Ranch Hand

Joined: Sep 08, 2004
Posts: 117
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

Joined: Sep 08, 2004
Posts: 117
Ritu varada
Ranch Hand

Joined: Sep 08, 2004
Posts: 117
I used java.net.encoder and decoder classes to encode and decode my url. That works fine. Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Retrieve cookie values