By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,191 Members | 1,256 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,191 IT Pros & Developers. It's quick & easy.

How to send Javascript variable to jsp page using AJAX?

P: 1
I have tried sending the value as shown(code), but no value is being caught in JSP.

It is showing null value in jsp page.

this is javascript/ajax code

Expand|Select|Wrap|Line Numbers
  1. var emp = document.getElementById("Employee").value;
  2. var url_temp = "print_emp.jsp?Employee="+emp;
  3. alert(url_temp);
  4.  
  5.  
  6. var xmlhttp;
  7. if (window.XMLHttpRequest)
  8. {
  9.     // code for IE7+, Firefox, Chrome, Opera, Safari
  10.     xmlhttp=new XMLHttpRequest();
  11. }
  12. else
  13. {// code for IE6, IE5
  14.     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  15. }
  16. xmlhttp.onreadystatechange=function()
  17.     {
  18.  
  19.     if (xmlhttp.readyState==4 && xmlhttp.status==200)
  20. {
  21.         document.getElementById("result_display").innerHTML=xmlhttp.responseText;         
  22.           //document.getElementById(result_display).innerHTML="print_emp.jsp";
  23. }
  24. }
  25. xmlhttp.open("GET",url_temp,true);
  26. xmlhttp.send();
  27.  
  28.  
  29. trying to read in jsp page as shown below, but no use.
  30.  
  31. String employee=request.getParameter("emp");
  32. System.out.println("this is employee value"+employee);
Jan 22 '15 #1
Share this Question
Share on Google+
1 Reply


100+
P: 107
Hi,

Please follow the rules, provide your code in CODE tag.

And Please ask questions clearly, from which JSP u r callind this javascript method and how do u want to navigate to print_emp.jsp?

Try to use hidden field

Expand|Select|Wrap|Line Numbers
  1.  
  2. <input type="hidden" name="emp" id="emp">
  3.  
  4.  
In Javascript Code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. document.getElementById("emp").value = document.getElementById("Employee").value;
  3.  
  4. redirect/navigate to JSP
  5.  
  6.  
Jan 22 '15 #2

Post your reply

Sign in to post your reply or Sign up for a free account.