bliss
bliss

Reputation: 330

Reading input with ajax and Java servlet

i am new to java and ajax and servlets. i wrote a program that reads input from the user and prints out the dictionary meaning to the web page with the help of some tutorials.

When i read the input from the webpage in doPost method of the servlet, it fails to read it and returns null. maybe it is trying to read the input before the submit button. How am i going to solve this? Here is the related code of my jsp file:

function ajaxFunction() {
  if(xmlhttp) {
  var inword = document.getElementById("inputWord");
    xmlhttp.onreadystatechange  = handleServerResponse;
    xmlhttp.open("GET","gettime?inputWord="+ inword.value , true ); //gettime will be the servlet name
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send(null);
  }
}

...

<form name="myForm">
Enter the word: <input type="text" name="inputWord" />
<br />
Meaning:<input type="text" name="time" />
<br />
<input type="button" onClick="javascript:ajaxFunction();" value="Click to get the Meaning on Textbox"/>
<br />
</form>

And here is the part of the code that i'm trying to get the input in my servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String inWord = request.getParameter("inputWord"); // word to search in the dictionary
        PrintWriter out = response.getWriter();
...

everytime i try to run the project request.getParameter("inputWord"); returns null.

i tried some combinations of the code here xmlhttp.open("GET","gettime?inputWord="+ inword.value , true ); like xmlhttp.open("GET","gettime", true ); but didn't work.

and also i call doPost(request,response); in my doGet method.

any help is appreciated.

Upvotes: 1

Views: 1284

Answers (2)

slc84
slc84

Reputation: 61

I find that for AJAX issues it is sometimes useful to be able to see what is actually being sent to the server so that you can verify that it is what you expect.

I usually use Firefox and a useful addon called Firebug (which I don't do any web development without). It allows you to see the Ajax requests as they are made so that you can check that you are sending the correct information and that it is structured correctly..

In this case you might have noticed that you weren't sending the "inputWord" correctly as you don't have an ID on input:

<input type="text" name="inputWord" id="inputWord" />

Upvotes: 1

Peter Bratton
Peter Bratton

Reputation: 6408

Since you're using document.getElementById(), try setting an id attribute onto the inputWord control in your HTML:

<form name="myForm">
Enter the word: <input type="text" name="inputWord" id="inputWord" />
<br />
Meaning:<input type="text" name="time" />
<br />
<input type="button" onClick="javascript:ajaxFunction();" value="Click to get the Meaning on Textbox"/>
<br />
</form>

More information on document.getElementById():

http://www.tizag.com/javascriptT/javascript-getelementbyid.php

Upvotes: 1

Related Questions