Pass Javascript Value to Java in JSP
Solution 1
You cannot make a server call directly. You need to make a server request.
javascript plays on client side and JSP plays on server side.
What you need is you have to make a server request. And send that string as a query parameter.
Two options to achieve this.
Do not confuse that JSP and java script existed on same document(or file). Yes but JSP part compiles on server side and JavaScript executes by browser.
Solution 2
Javascript statements are rendered by Browsers and executed as client program.
In your case if you want execute a java code based on selection of html component, you would need to use ajax
call .
You can find quick example here
A Simple AJAX with JSP example
http://www.programming-free.com/2013/03/ajax-fetch-data-from-database-in-jsp.html
Mark Karan
Updated on June 04, 2022Comments
-
Mark Karan almost 2 years
I would like to pass a Javascript value to a Java function in JSP. How can I do that? The id comes from a combobox in JSP via Javascript. I will get the ID from ComboBox and send it to Java function as parameter to get the Java result.
function Display() { var IdFromCB = (document.getElementById("MListSelect")).value; //CALL JAVA FUNCTION HERE BY USING IdFromCB as function parameter //'<% getSomething(-----IdFromCB-----);%>' }
Thanks,Mark