• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setting and retrieving cookies using javascript on a jsp

 
Nina Binde
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64958
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nina Binde
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64958
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic