943,664 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Unsolved
  • Views: 67099
  • JSP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 20th, 2006
0

how can i pass a value from java script to JSP scriplet

Expand Post »
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:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
venkatnavi is offline Offline
2 posts
since Jun 2006
Jun 20th, 2006
0

Re: how can i pass a value from java script to JSP scriplet

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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jun 20th, 2006
0

Re: how can i pass a value from java script to JSP scriplet

Quote 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..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
venkatnavi is offline Offline
2 posts
since Jun 2006
Jun 21st, 2006
0

Re: how can i pass a value from java script to JSP scriplet

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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Sep 16th, 2006
0

Re: how can i pass a value from java script to JSP scriplet

Can u tell me how did u solved the problem?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vijaysdon is offline Offline
4 posts
since Sep 2006
Sep 16th, 2006
0

Re: how can i pass a value from java script to JSP scriplet

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).
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Sep 17th, 2006
0

Re: how can i pass a value from java script to JSP scriplet

Thanks for the suggestion I will try it and hope that will be the solution for my problem. Thanks a looooot again
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vijaysdon is offline Offline
4 posts
since Sep 2006
Sep 21st, 2006
0

Re: how can i pass a value from java script to JSP scriplet

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
markenranosa is offline Offline
6 posts
since Sep 2006
Oct 6th, 2006
0

Re: how can i pass a value from java script to JSP scriplet

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..
Reputation Points: 10
Solved Threads: 1
Newbie Poster
hudz is offline Offline
24 posts
since Jan 2006
Oct 6th, 2006
0

Re: how can i pass a value from java script to JSP scriplet

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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: JSTL - Obtaining values from url string
Next Thread in JSP Forum Timeline: Whats Wrongin folloowind code for connecting with mysql





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC