954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Passing variable value from javascript to jsp page at run time

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,...)

vijaysdon
Newbie Poster
4 posts since Sep 2006
Reputation Points: 10
Solved Threads: 0
 

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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Request all developer to send codes....

Thanks in advance

ukbasak
Newbie Poster
8 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Request all developer to send codes....

Thanks in advance

What a maroon.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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>
<head>






</head>



<body>
<script type="text/javascript">function ajaxFunction()
{var xmlHttp;
try
  {  // Firefox, Opera 8.0+, Safari 
   xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {  // Internet Explorer 
   try
    {    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    }
  catch (e)
    {    try
      {      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      }
    catch (e)
      {      alert("Your browser does not support AJAX!");      return false;      }    }  }
  xmlHttp.onreadystatechange=function()
    {
	   //alert("i m  not  in ready state");
	   
	    if(xmlHttp.readyState==0)
      {
      //document.myForm.time.value=xmlHttp.responseText;
	    alert("The request is not initialized");
	  
	   

	   
	  
      }
	      if(xmlHttp.readyState==1)
      {
      //document.myForm.time.value=xmlHttp.responseText;
	    alert("The request has been set up");
	  
	   

	   
	  
      }
	  
	  
	  
	        if(xmlHttp.readyState==2)
      {
      //document.myForm.time.value=xmlHttp.responseText;
	    alert("The request has been sent");
	  
	   

	   
	  
      }
	  
	  	        if(xmlHttp.readyState==3)
      {
      //document.myForm.time.value=xmlHttp.responseText;
	    alert("The request is in process");
	  
	   

	   
	  
      }
	   
	  
	   
	  
	  
	   
	   
	  
    if(xmlHttp.readyState==4)
      {
      //document.myForm.time.value=xmlHttp.responseText;
	    //alert("i m in ready state");
	  
	   document.getElementById("theResponse").innerHTML = xmlHttp.responseText;

	   
	  
      }
    }
	      var gender=document.myForm.gender.value;
	      var url="getAllGender.jsp";
          url=url+"?gender="+gender+"&sid="+Math.random();
          //url=url+"&sid="+Math.random();

	
	  
         xmlHttp.open("GET",url,true);
         xmlHttp.send(null); 
   }
  
  </script>

  <form name="myForm">
 <select   name="gender"  onChange="ajaxFunction(); ">
<option  >Select gender</option>
<option>Male</option>


<option>Female</option>

</select>



<div id="theResponse">

</div>
</form></body>
</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

amarjeetsingh
Newbie Poster
21 posts since Feb 2009
Reputation Points: 2
Solved Threads: 1
 
  • Is there any problem between you and use of code tags that are required by this forum?
  • 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
peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

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,...)

chethandas
Newbie Poster
2 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

If you took your time to read answer by masijade you wouldn't have to post this question.

Thread closed.

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You