Home arrow static arrow Java Programming [Archive] - Passing Data from Java to JavaScript
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Passing Data from Java to JavaScript
This topic has 5 replies on 1 page.

Posts:833
Registered: 1/2/04
Passing Data from Java to JavaScript  
Jul 7, 2004 5:13 AM



 
Hi,

I am writing a servlet, I have a JComboBox and onChange event, I want to submit a form with an extra parameter. Can anybody tell me how to do this.

I am doing this but the value of refresh variable is 0.

out.println("<td><select name='groupid' onchange='refresh=1; submit();'>");


Thanks
 

Posts:8,604
Registered: 29/04/03
Re: Passing Data from Java to JavaScript  
Jul 7, 2004 5:20 AM (reply 1 of 5)



 
Javascript variables won't submit to the server - only html form/input fields will.

Where is the refresh field?
Is it a hidden field on the page, or something else?

<input type="hidden" name="refresh" value="0">

and
onchange='document.forms[0].refresh.value = 1; submit()'
 

Posts:5,627
Registered: 3/22/04
Re: Passing Data from Java to JavaScript  
Jul 7, 2004 5:30 AM (reply 2 of 5)



 
In my servlet, I put some data in a well-known repository (read Session). Then in my corresponding JSP, I retrieve said data.
<SCRIPT LANGUAGE="JavaScript1.2">var myJavaScriptVar = '<%=session.getAttribute("userID")' %>;</SCRIPT>


And look, Model 2 architecture is not broken!
 

Posts:5,627
Registered: 3/22/04
Re: Passing Data from Java to JavaScript  
Jul 7, 2004 5:33 AM (reply 3 of 5)



 
Oh, and by the way, servlets don't have JComboBoxes, they may have
<SELECT...>
.
 

Posts:833
Registered: 1/2/04
Re: Passing Data from Java to JavaScript  
Jul 7, 2004 5:33 AM (reply 4 of 5)



 
Javascript variables won't submit to the server - only
html form/input fields will.

Where is the refresh field?
Is it a hidden field on the page, or something else?

<input type="hidden" name="refresh" value="0">

and
onchange='document.forms[0].refresh.value = 1;
submit()'

Thanks, it works.
 

Posts:122
Registered: 3/27/03
Re: Passing Data from Java to JavaScript  
Jul 7, 2004 5:41 AM (reply 5 of 5)



 
just my two cents,

for a generic form post I would generally do this,

<script language="javascript">
function postMyForm(){
document.formName.action = "myServeletAddress";
document.formName.method = "post";
document.formName.submit();
}
</script>

<form name="formName" onSubmit="return false;">
<select name="groupid" onChange="postMyForm();">
...
</select>
</form>
 
This topic has 5 replies on 1 page.