0

I want to pass a javascript variable to a java servlet. I am developing a web application. This is my html code:

<p id="test">Some text</p>

And this is what i write in the javascript file:

var myVar = document.getElementById('test').innerText;

$.ajax({
url: 'Test',
data: {
    myPostVar: myVar
},
type: 'POST'
});

And then this in the servlet (in the doGet):

String result = request.getParameter("myPostVar");
System.out.print(result);

And if i run the Test.java to test, it gives me "null". I googled too much but could not find any solution.

user2959870
  • 80
  • 6
  • 25
  • 1
    Same question as this http://stackoverflow.com/questions/28100431/how-to-pass-a-javascript-variable-to-a-java-servlet/28104745#28104745 , didnt you get the answer – Santhosh Jan 23 '15 at 08:53
  • Anyway you have to submit the form. Better make an AJAX POST call and on servlet get the parameter. Same as mentioned by San. – Vaibs Jan 23 '15 at 09:07
  • No i did not get the answer. maybe you know some other solution that could work – user2959870 Jan 23 '15 at 09:09
  • Use this into your javascript `window.location.href = 'url?parameter=' + myval` – Alist3r Jan 28 '15 at 10:08

1 Answers1

0

You use doGet instead of doPost

type: 'POST'

Change it to GET or use doPost in your Java project