I am trying to send a JavaScript variable to a Java servlet in a web application I am developing. Below is the HTML code:
<p id="test">Some text</p>
Here is the JavaScript code I am using:
var myVar = document.getElementById('test').innerText;
$.ajax({
url: 'Test',
data: {
myPostVar: myVar
},
type: 'POST'
});
Then, in the servlet's doGet method, I have the following code:
String result = request.getParameter("myPostVar");
System.out.print(result);
However, when I test the Test.java file, it returns "null". I have searched extensively online for a solution but have not been able to find one.