Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing values from JavaScript to Java

Status
Not open for further replies.

Ajith2002

Programmer
Jul 16, 2002
30
US
Hi folks,

How do we pass a value from a JavaScript code to Java code, within the same JSP page ?

I want to execute a Java code using a value I get from a JavaScript function(inside same JSP page).

Thanks very much in advance.
 
I suppose you could put the value in the Session then the other code could retrieve it. But why are you using two different languages in a single JSP page? Actually I didn't even know that was supported.

-pete
 


Sicne javascript isd use for client side testing surely the best solution is to use it as it is designed.

e.g. take input from text boxes
use javascript to validate
pass to handling page,

why else would you want to do this.
 
I completely agree with jaydar.Javascript shouldn't make business.Instead it must be used to validate the form info and send the info to java (servlet for example) Salih Sipahi
Software Engineer.
City of Istanbul Turkey
ssipahi@yonbilgi.com
 
Hi Ajith,

Those most of them have not provided u with perfect answer,I have ANSWER FOR U.
Have a hidden text box and provide the value of the javascript function into the text box.Now in JSP page try to resubmit the page so that now the ,u can retrive the value using request.getParameter("Hidden textbox").

I think this would provide u with perfect solution.
Revert back if not clear.

Vishnu
 
Hello Ajith,

Here is an easy way to pass information from javascript to jsp. Like other answers, you need to do it going from one page to another. I do this by passing it through the URL. Then in the next page I get it with a:

Code:
request.getParameter("variable");

this javascript function is in the dreamweaver library.
Code:
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+&quot;.location='&quot;+args[i+1]+&quot;'&quot;);
}
Then I set up a button like this.
Code:
<input name=&quot;name&quot; type=&quot;button&quot; onClick=&quot;MM_goToURL('parent','somepage.jsp?variable=' + document.form.textbox.value);&quot; value=&quot;click&quot;>
the javascript builds the link with the value of the text box to send to the second jsp page. It will work with any form element.

It doesn't have to be a button either. just a place you can use the onClick.

I hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top