| Author |
Ajax: servlet called twice
|
Stanley Walker
Ranch Hand
Joined: Sep 23, 2009
Posts: 72
|
|
i didnt knwo where to put in my ajax queries so i just put it in here. hope its alright :P
i was trying a simple ajax code, where i am simply writing a text returned from a servlet to the html form.
my jsp is as below:
/********************* jsp code**********************************/
/********************* jsp code**********************************/
and my servlet is as below
/********************* servlet code**********************************/
/********************* servlet code**********************************/
i have a couple of questions regarding the above:
1. why do we have write the following in the html code:XMLHttpRequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); i did not give this initally but then i found that at the servlet my loginid and password is coming as null
2. my servlet gets called twice. the desired result gets printed twice, and then sudden;y low and behold another time my servlet gets called, that too the doget() method. any clues.
please help i have been struggling for some time now. any help would be appreciated.
any questions on the above code would also be gladly answered
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56150
|
|
Moved to the HTML/Javascript forum.
Also, please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.
You can go back and change your post to add code tags by clicking the button on your post.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Chitresh Katarpawar
Greenhorn
Joined: Feb 08, 2010
Posts: 2
|
|
|
[Thread hijack removed. Please post your own questions in your own topic.]
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56150
|
|
Stanley, are you trying to learn the inner workings of Ajax, or are you trying to get things done?
If the latter, be aware that very few experienced developers "roll their own" Ajax like this. There are just too many nuances to take care of. Rather, one of the JavaScript libraries like jQuery or Prototype will be employed.
If you're willing to go that route, I'm willing to bet that your problems will just magically go away.
|
 |
Stanley Walker
Ranch Hand
Joined: Sep 23, 2009
Posts: 72
|
|
i was actually trying to learn the inner workings of ajax on my own and hence wrote a very very simple ajax applications after going thru a book. but even such a simple application is not working. i mean ajax is doing its bit by submitting form without refreshing page but for some reason my servlet is getting hit twice something i simply cannot figure out.
i thought maybe going down the route of frameworks would be advisable after doing a little tinkering of my own, hence did not learn about json or prototype frameworks.
am i doing things the wrong way.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56150
|
|
Well, your servlet is getting hit twice because you are sending two requests. No surprise there.
Why are you puzzled because the servlet is hit twice? If you send two requests, you'll get two servlet invocations.
|
 |
Stanley Walker
Ranch Hand
Joined: Sep 23, 2009
Posts: 72
|
|
well i figured that but even if i do this
XMLHttpRequestObject.send("loginId="+document.forms[0].loginId.value+"&password="document.forms[0].password.value);
still servlets gets hit twice.
i am puzzled there
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56150
|
|
|
So you are saying that reducing the number of send() calls to one still invokes the servlet twice? I'm skeptical. Are you sure you are not running an old cached version of the page?
|
 |
Stanley Walker
Ranch Hand
Joined: Sep 23, 2009
Posts: 72
|
|
yes it does.
something i am doing wrong. even if i do
XMLHttpRequestObject.setRequestHeader('Connection','close');
so that connection is closed as and when a response is received , even then.
where am i going wrong.?? i am starting to begin if i can ever get this code working.
|
 |
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15357
|
|
Well problem you have is you are not canceling the form submission.
Eric
|
 |
Stanley Walker
Ranch Hand
Joined: Sep 23, 2009
Posts: 72
|
|
eric, please tell me how do i cancel the form submmission.
and also does this mean that when we submit a form using ajax, the form keeps on submitting unless we do something specific about it??
|
 |
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15357
|
|
You return false, you really should use onsubmit on the form and not an onclick handler. You should not use javascript: in the handlers. And yes the html form has no clue about JavaScript. There is no secret handshake that says hey I am Ajax, do not submit.
Eric
|
 |
 |
|
|
subject: Ajax: servlet called twice
|
|
|