| | |
how can i pass a value from java script to JSP scriplet
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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.
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.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Jun 2006
Posts: 2
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by masijade
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.
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.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
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).
<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).
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Sep 2006
Posts: 6
Reputation:
Solved Threads: 0
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
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..
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..
-noIdea-
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.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
Other Threads in the JSP Forum
- Previous Thread: JSTL - Obtaining values from url string
- Next Thread: Whats Wrongin folloowind code for connecting with mysql
| Thread Tools | Search this Thread |
apache array backbutton combobox comma connection csv database development directorystructure dropdownlist dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 mysql netbeans network parameters passing ping printinserverinsteadofclient read redirect request.getparameter response seperated servlet servletdopost()readxml sessions software sql ssl state_saving_method stocks sun tomcat tutorial update values video web write






