aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes setting and retrieving cookies using javascript on a jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "setting and retrieving cookies using javascript on a jsp" Watch "setting and retrieving cookies using javascript on a jsp" New topic
Author

setting and retrieving cookies using javascript on a jsp

Nina Binde
Ranch Hand

Joined: Sep 24, 2004
Posts: 85
Hi,

I need to set a cookie on a jsp with some value stored in a java variable "adID". I would need to do that after the page loads, so I use:


I would need to retrieve the cookie value on another jsp. I have the retrieve method for the cookie in javascript.

What I am not sure is how I can use this method in my jsp to retrieve the value of the cookie 'Name' into a java variable?

Any help on this is appreciated. Thank you.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61418
    
  67

Originally posted by Nina Binde:
What I am not sure is how I can use this method in my jsp to retrieve the value of the cookie 'Name' into a java variable?

You can't save anything into a Java variable from javaScript. Think about it for a minute. The Java in the JSP executes on the server long before the page is sent to the browser where the JavaScript will execute. The Java no longer exists, so how could JavaScript interact with it?

If that's not clear, you might find this article helpful.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nina Binde
Ranch Hand

Joined: Sep 24, 2004
Posts: 85
Got it. Thank you, Bear.
Instead, I would like to now read the cookie value in javascript and do a check (if there is cookie present, then do this. Else, do that.)
Is this possible and if yes, how can I embed this javascript code in the java without an event being triggered?

Thank you for your replies.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61418
    
  67

Originally posted by Nina Binde:
how can I embed this javascript code in the java without an event being triggered?

I'm not sure what you mean by this. Again, the Java executes on the server, the JavaScript executes on the client. They don't have much to do with each other.

Perhaps you should back up a bit and explain what you are really trying to accomplish, and maybe we can suggest an appropriate approach.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
http://search.live.com/results.aspx?q=JavaScript+getCookie

There are plenty examples out on the net on how to read a cookie with JavaScript.

Eric
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setting and retrieving cookies using javascript on a jsp