943,625 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Unsolved
  • Views: 64143
  • JSP RSS
Sep 16th, 2006
-2

Passing variable value from javascript to jsp page at run time

Expand Post »
How can we pass the variable value from javascript to jsp page at runtime? I am getting this problem when i want to display the second drop down box according to the selected item in the first drop down.
For ex: Lets consider we have a drop down field named Gender(Male,Female).
If we select Male then the next Drop Down has to be shown as (Kumar, Mr,...)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vijaysdon is offline Offline
4 posts
since Sep 2006
Sep 16th, 2006
0

Re: Passing variable value from javascript to jsp page at run time

You can't. JSP runs on the server, Javascript (which is NOT Java) runs on the client. You might want to look into something like AJAX which will allow your javascript to make a complete connection to the server, receive data, and then update your page, which will make it look like you are doing what you are asking, but it takes longer than a true programmatic connection.

P.S. Don't come back and ask for more info concerning AJAX. That is another thing does not belong on this forum.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Apr 23rd, 2009
0

Re: Passing variable value from javascript to jsp page at run time

Request all developer to send codes....

Thanks in advance
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ukbasak is offline Offline
8 posts
since Nov 2008
Apr 23rd, 2009
0

Re: Passing variable value from javascript to jsp page at run time

Click to Expand / Collapse  Quote originally posted by ukbasak ...
Request all developer to send codes....

Thanks in advance
What a maroon.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Apr 24th, 2009
0

Re: Passing variable value from javascript to jsp page at run time

Yes u can do it

but u hv to speicify from where do u want
to display the list of all the the male members

if u want that value to be get from the database
then u can use ajax from ur current jsp page


This is the Demo for ur help

html Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9. </head>
  10.  
  11.  
  12.  
  13. <body>
  14. <script type="text/javascript">function ajaxFunction()
  15. {var xmlHttp;
  16. try
  17. { // Firefox, Opera 8.0+, Safari
  18. xmlHttp=new XMLHttpRequest();
  19. }
  20. catch (e)
  21. { // Internet Explorer
  22. try
  23. { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
  24. catch (e)
  25. { try
  26. { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
  27. catch (e)
  28. { alert("Your browser does not support AJAX!"); return false; } } }
  29. xmlHttp.onreadystatechange=function()
  30. {
  31. //alert("i m not in ready state");
  32.  
  33. if(xmlHttp.readyState==0)
  34. {
  35. //document.myForm.time.value=xmlHttp.responseText;
  36. alert("The request is not initialized");
  37.  
  38.  
  39.  
  40.  
  41.  
  42. }
  43. if(xmlHttp.readyState==1)
  44. {
  45. //document.myForm.time.value=xmlHttp.responseText;
  46. alert("The request has been set up");
  47.  
  48.  
  49.  
  50.  
  51.  
  52. }
  53.  
  54.  
  55.  
  56. if(xmlHttp.readyState==2)
  57. {
  58. //document.myForm.time.value=xmlHttp.responseText;
  59. alert("The request has been sent");
  60.  
  61.  
  62.  
  63.  
  64.  
  65. }
  66.  
  67. if(xmlHttp.readyState==3)
  68. {
  69. //document.myForm.time.value=xmlHttp.responseText;
  70. alert("The request is in process");
  71.  
  72.  
  73.  
  74.  
  75.  
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. if(xmlHttp.readyState==4)
  86. {
  87. //document.myForm.time.value=xmlHttp.responseText;
  88. //alert("i m in ready state");
  89.  
  90. document.getElementById("theResponse").innerHTML = xmlHttp.responseText;
  91.  
  92.  
  93.  
  94. }
  95. }
  96. var gender=document.myForm.gender.value;
  97. var url="getAllGender.jsp";
  98. url=url+"?gender="+gender+"&sid="+Math.random();
  99. //url=url+"&sid="+Math.random();
  100.  
  101.  
  102.  
  103. xmlHttp.open("GET",url,true);
  104. xmlHttp.send(null);
  105. }
  106.  
  107. </script>
  108.  
  109. <form name="myForm">
  110. <select name="gender" onChange="ajaxFunction(); ">
  111. <option >Select gender</option>
  112. <option>Male</option>
  113.  
  114.  
  115. <option>Female</option>
  116.  
  117. </select>
  118.  
  119.  
  120.  
  121. <div id="theResponse">
  122.  
  123. </div>
  124. </form></body>
  125. </html>

but in getAllGender.jsp
u hv to get the data from the datebase
now it is up to u
i gave u the hint
no much time for me dear
i hope this is very benicifial for u
Last edited by peter_budo; Apr 24th, 2009 at 8:43 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 2
Solved Threads: 1
Newbie Poster
amarjeetsingh is offline Offline
21 posts
since Feb 2009
Apr 24th, 2009
1

Re: Passing variable value from javascript to jsp page at run time

  1. Is there any problem between you and use of code tags that are required by this forum?
  2. If you took time to read previous replies you found find that
    • Post is old and revoked by somebody who just seek quick solution to his school assignment or plunge the hole in what ever he told his employer about his experiences and knowledge
    • Use of Ajax was mentioned, but considered as of topic or different point of implementation in this scenario, therefore not discussed
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is online now Online
6,654 posts
since Dec 2004
May 5th, 2010
0

using javascript variable in jsp

How can we pass the variable value from javascript to jsp page at runtime? I am getting this problem when i want to display the second drop down box according to the selected item in the first drop down.
For ex: Lets consider we have a drop down field named Gender(Male,Female).
If we select Male then the next Drop Down has to be shown as (Kumar, Mr,...)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chethandas is offline Offline
2 posts
since May 2010
May 5th, 2010
1
Re: Passing variable value from javascript to jsp page at run time
If you took your time to read answer by masijade you wouldn't have to post this question.

Thread closed.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is online now Online
6,654 posts
since Dec 2004

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.
This thread is currently closed and is not accepting any new replies.
Previous Thread in JSP Forum Timeline: exe in jsp
Next Thread in JSP Forum Timeline: WinHttp object and posting





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


Follow us on Twitter


© 2011 DaniWeb® LLC