Hi,
This is naveen,
i am having a problem that, i am having a combo box in the JSP page when i select an item from that i am calling the onchange function.

In javascript i am getting the value now i want to pass this value to JSP scriptlet..


please help me..

Thanks in advance
Naveen.:eek:

Recommended Answers

All 14 Replies

Javascript runs on the client, JSP runs on the server. They cannot
communicate with each other directly. If you are advanced enough, look
into a servlet web service and AJAX to solve your problem, if it must
absolutely be dynamic. Otherwise, you are going to have to submit your form.

Javascript runs on the client, JSP runs on the server. They cannot
communicate with each other directly. If you are advanced enough, look
into a servlet web service and AJAX to solve your problem, if it must
absolutely be dynamic. Otherwise, you are going to have to submit your form.

Thank you,
for your reply..

i will try out that AJAX ....

i submitted the form and got the value and now working fine..
again here i got problem that i am not able to send the values from the action to the JSP page from which i have submitted.


it is giving null value in java script..

If you mean passing a variable that only exists as a javascript variable,
you cannot. What you can do, however, is to provide a hidden input
field in your form, and write the value of the variable to this input field
before you submit your form.

Can u tell me how did u solved the problem?

With a field as follows:

<INPUT TYPE="HIDDEN" NAME="variableName" VaLUE="">

Then in Javascript you can do:

document.form.variableName.value="value";
form.submit();

and the JSP can evaluate the param "variableName". Remember though, that a Parameter is always a String (or an array of strings if accessed through a different method) so you will need a way to transform this string to what you want, if you want something other than a String (e.g. an Integer).

Bt to tell you the truth I am not sure what you by a null value in javascript. Don't forget that anything that the JSP reads from the params, must then turn around and insert the value back into the hidden field to be read by the javascript on the client (or actually modify the javascript in the JSP before returning the html to the client).

Thanks for the suggestion I will try it and hope that will be the solution for my problem. Thanks a looooot again

i too have a shorter method maybe you wanna try it out.
i also use a hidden variable and i retrive it using scriplet,.. i am still a newbie am 19 but i hope this helps. ^_^ here it is

<script>
function GetVar(variableValue){
document.location.href = "TestProject/ShowForm.do?variableInJSP="+variableValue;
}
</script>


__________________________________________________
<html:hidden property="variable" name="FormValueName"/>

<%
String var = request.getParameter("variable");
%>

then us can use var by calling it in the javascript like :

onclick="GetVar('<%=var%>')"; <!-- // hehe i got lazy but you know where to place this -->

i hope it helps a bit

Ken - Ken
Legazpi City, Bicol, Philippines

but the structure above isn't dynamic enough.

What if you have this kind of situation:

<script language="JavaScript">


function sendVar(val){
document.form.pob1.value = val;


function show(id)
{
if (document.getElementById(id).style.display == 'none')
{
document.getElementById(id).style.display = '';
}
}


function hide(id)
{
document.getElementById(id).style.display = 'none';


}
}
</script>

In JSP

<table id="0">
<html:select name=country onchange="show('1'),hide('2');"/>
</table>


<table id="1" style="display:none;">
<html:select name="pob" onchange="setVar(this.value);"/>
</table>


<table id="2" style="display:none;">
<html:select name="pob" onchange="setVar(this.value);"/>
</table>


<input type="hidden" name="pob1" value="">

But, eventually Javascript didn't send the value to hidden name=pob1

I'm out of idea..

hehe..

Your JavaScript in the above post is malformed. Look at the first function. It isn't ended until the } at the very end of the JavaScript section. That is probably why you are having problems.

thanks for replying. But this is isn't my real code. im only write it down as simple as possible. but the psuedocode is like that. I hope you understand. But if you want the real code I can just copy and paste. Tq.

JAVASCRIPT
<script language="JavaScript">
function show(id)
{
if (document.getElementById(id).style.display == 'none')
{
document.getElementById(id).style.display = '';
}
}

function hide(id)
{
document.getElementById(id).style.display = 'none';

}

function change(type,countryId,tableId1, tableId2,tableId3)
{

if(type == 'RACE'){
if(countryId == 'BUMI' || countryId == 'CHI' || countryId == 'IND' ||countryId == 'BSBH'|| countryId == 'BSWK' )
{
//alert('in');
show(tableId1);hide(tableId2);hide(tableId3);show('ethnicTbl'); hide('err');
}
else{
//alert('out');
show(tableId2);hide(tableId1); hide(tableId3); hide('ethnicTbl');show('err');
}
}

else if (type=='ADD' || type=='NATIONALITY'){
if(countryId == '222')
{
//alert('in');
show(tableId1); hide(tableId2); hide(tableId3);
}
else{
//alert('out');
show(tableId2); hide(tableId1); hide(tableId3);
}
}

}

function sendToBean(type,val){
alert(val + type);
if(type == 'pob'){
document.pobForms.pob3.value = val;
}
}

</script>

in JSP

<html:select name="applicationVO" property="nationalityId"  onchange="change('NATIONALITY',this.options[this.selectedIndex].value,'pobStateTbl','pobStateIntTbl','');">
        <html:options collection="nationlv" name="nationVO" property="nationalityId"
                          labelName="nationVO" labelProperty="nationalityName" />
    </html:select>

 <table id="pobStateIntTbl" style="display:none;" cols=1 cellpadding=2>
        <tbody><tr valign=top align=left><td  valign=baseline></td> <td width=1 style='width:250px;'>
              <html:select property="pob" onchange="sendToBean('pob',this.value);">
                <html:options collection="countrylvPob" name="countryVO" property="countryName"
                          labelName="countryVO" labelProperty="countryName" />
              </html:select> 
            </td></tr> </tbody> </table>
      <table id="pobStateTbl" style="display:none;" cols=1 cellpadding=2>
        <tbody> <tr valign=top align=left> <td  valign=baseline></td> <td width=1 style='width:250px;'>
              <html:select property="pob"  onchange="sendToBean('pob',this.value);">
                <html:options collection="statelvPob" name="stateVO" property="name" 
                         labelName="stateVO" labelProperty="name" />
              </html:select>
            </td> </tr></tbody> </table>
     
	  <c:choose> <c:when test="${applicationVO.nationalityId == '222' }">
	  <script type="text/javascript">change('NATIONALITY','222','pobStateTbl','pobStateIntTbl','');</script>
     </c:when> <c:otherwise>
      <script type="text/javascript">change('NATIONALITY','221','pobStateTbl','pobStateIntTbl','');</script>
      </c:otherwise> </c:choose>
	
	 <form name="pobForm">
	 <input type="hidden" name="pob3" value="">
	 </form>

THE PROBLEM(s)

1. Only 1st table (id=pobStateIntTbl) is sent to action.java when submit();
2. Javascript seems don't send value to pobForm.pob3.value (hidden field) and it is declare using html not jstl.

Thank you..:)

got it..

somehow,

document.pobForms.pob3.value = val;

did not sent val to pob3.value

then i removed pobForms and replaced with forms[0]

document.forms[0].pob3.value = val;

and then the magic begins..I just don't understand why javascript didn't recognise my pobForms previously. Can someone explain?

hi venkat

i have a similar issue.
i am calling a javacript from a java code and i need to return a value from the javascript file to the calling java file.
kindly share your suggestions with me

thanks

hi venkat

i have a similar issue.
i am calling a javacript from a java code and i need to return a value from the javascript file to the calling java file.
kindly share your suggestions with me

thanks

Hi Komathi,

First, you must understand that Javascript is running on client side i.e. At your browser. Meanwhile Java code is running on your server side i.e. At your server.

So, it means that Java code can send value to javascript at the first time it rendered by browser.

Example:

<script>var javascriptValue = <%=valueFromYourJavaCode%></script>

But Javascript can't send back to Java code as it is running only at browser.

<script>
<%String javaValue = %> valueFromYourJavascriptCode <%;%>
</script>

tq.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.