4

I want to get the parameter from a input form which is set on my index.html:

GET:<br> 
<input type="text" size="20" id="name2" onblur="validate2()"  
     onFocus = "document.getElementById('msg2').innerHTML = ' '">
<div id = "msg">&nbsp</div>

On my servlet I want to get this parameter by request.getparameter("name2")

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("Get");
    System.out.println(request.getParameter("name2"));
    if(!request.getParameter("name2").equals("")) {
        numer = request.getParameter("name2");
        serviceConnection(request, response);
    }
}

but when I am starting my application the system.out.println is just printing the null variable.

On my ajaxvalidator javascript file I wrote this:

function validate2() {
var idField = document.getElementById("name2");
var data = "name2=" + encodeURIComponent(idField.value);
if (typeof XMLHttpRequest != "undefined") {
    req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
}
var url = "Validator"
req.open("GET", url, true);
req.onreadystatechange = inserter2
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
req.send(data);

}

function inserter2() {
    if (req.readyState == 4) {
        if (req.status == 200) {
            var msg1 = req.responseText
            if (msg1 == "") {
                document.getElementById("msg").innerHTML = "<div style=\"color:red\">Wadliwa nazwa</div>";
                document.getElementById("org").value = '';
            } else {
                document.getElementById("org").value = msg1;
            }
        }
    }

How to solve this problem?

Ganjira
  • 936
  • 5
  • 15
  • 31
  • Check you browser console for any errors. Also check the form data sent to the servlet in the browser check if the value is being sent from there. – underdog Apr 26 '15 at 16:38
  • @underdog, I edited my question and added the code from my javascript file. – Ganjira Apr 26 '15 at 16:39
  • the code is your servlet code; I don't see anywhere the ajax code – underdog Apr 26 '15 at 16:40
  • @underdog right, sorry - my fault :) – Ganjira Apr 26 '15 at 16:44
  • did you check in the browser if the value is being sent to the server – underdog Apr 26 '15 at 16:46
  • @underdog the same situation - the console of netbeans still shows this parameter as null. – Ganjira Apr 26 '15 at 16:53
  • not the console of netbeans, console of the browser – underdog Apr 26 '15 at 16:54
  • @underdog well, i have quite same formula but for doPost, and there my program works properly, so it sends normally to the server – Ganjira Apr 26 '15 at 16:56
  • Did you try to print the value of `idField.value` to the browser console before sending? Try `console.log(idField.value);` to check the value. Also, perhaps try using FormData and see if that works. – chRyNaN Apr 26 '15 at 18:02
  • You seem to not have figured out the browser console yet. This is a very important thing to know when developing web applications. You can in Chrome/Firefox23+/IE9+ press F12 to get the developer toolset. Then you can click the "Network" (or "Net") tab to open up the HTTP traffic monitor. All HTTP traffic inclucing ajax requests are logged there. You can inspect the request URL, headers, body, etc and the response there. – BalusC Apr 27 '15 at 06:33

1 Answers1

0

Your mistake is here:

req.open("GET", url, true);
// ...
req.send(data);

In HTTP GET, the data needs to go in request URL query string, not in request body. Sending data in request body works for POST only. The request URL query string is the part after ? in request URL.

So, this should do:

req.open("GET", url + "?" + data, true);
// ...
req.send();

Note that you can remove the request body content type header.

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452