Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing a value from Javascript to JSP method?

 
zub kodi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my code and below is my explanation?

javascript code....

I need to pass a selected value from the Form Dropdown into a java setMethod() in my jsp page. But I can't seem to figure out how? I used the "setValue" javascript funtion as shown above, that passes the value from Javascript to jsp on a onClick event, but it doesn't work. I did some research on the internet and have learnt that I cannot pass values from Javascript to jsp.

Is there any other method, may be after I hit the submit button? I need to pass the selected value from a student dropdown list to a jsp setMethod(). So for example if I select, Jon from a Dropdown list which has a ID = 5, then I need to pass 5 to the java method.

Any Clues?

Thanks
[ April 03, 2008: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by zub kodi:
I need to pass a selected value from the Form Dropdown into a java setMethod() in my jsp page. But I can't seem to figure out how?

That's because you can't.
Any Clues?
Your JSP executes on the server in order to format the HTML page (including your script) to be sent to the client. By the time your JavaScript executes, the JSP has long since finished processing and has gone out of scope.

You might find this article helpful in understanding JSP concepts.
[ April 03, 2008: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To communicate with the server after the page has been loaded, you either need to submit the page back to the server, or send a request to the server via Ajax. As you will realize after reading the linked article, you can't just call Java methods from script -- especially any methods in the JSP that created the page.

(And before anyone gets the bright idea of mentioning DWR, that's just a special case of Ajax and should not even be considered before thoroughly understanding how HTTP, JSP and the whole Ajax ball of wax works.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic