IntelliJ open source
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Reply Bookmark it! Watch this topic JavaRanch » Forums » Engineering » HTML and JavaScript
 
RSS feed
 
New topic
Author

Ajax: servlet called twice

Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Messages: 34

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

This message was edited 2 times. Last update was at by Bear Bibeault

Bear Bibeault
Author and opinionated walrus
Sheriff

Joined: Jan 10, 2002
Messages: 36596

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
Messages: 2

[Thread hijack removed. Please post your own questions in your own topic.]

This message was edited 1 time. Last update was at by Bear Bibeault

Bear Bibeault
Author and opinionated walrus
Sheriff

Joined: Jan 10, 2002
Messages: 36596

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.

[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Messages: 34

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 opinionated walrus
Sheriff

Joined: Jan 10, 2002
Messages: 36596

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.

[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Messages: 34

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 opinionated walrus
Sheriff

Joined: Jan 10, 2002
Messages: 36596

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?

[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Messages: 34

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
Sheriff

Joined: Nov 08, 2001
Messages: 13583

Well problem you have is you are not canceling the form submission.

Eric


Race Schedule for 2010 for this running geek:
* National Half Marathon - 03/20 [1:44:51] * Frederick Marathon - 05/02 * Baltimore Marathon - 10/16 *
Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Messages: 34

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
Sheriff

Joined: Nov 08, 2001
Messages: 13583

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

Race Schedule for 2010 for this running geek:
* National Half Marathon - 03/20 [1:44:51] * Frederick Marathon - 05/02 * Baltimore Marathon - 10/16 *
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Engineering » HTML and JavaScript
 
RSS feed
 
New topic
IntelliJ open source

.