Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't call Servlet using Ajax

 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there !
I've been trying to set up a Simple Ajax Demo Application, but quite honestly, I have never been this frustrated before, because I can't get it to work. Could someone please help me, I've not yet met success... despite the amount of times I've modified this codes...
Here's the code for my Servlet:



And Here's the code for my xhtml:



I know I'm making more than one mistake, but the one that worries me most, is the fact that My Servlet never gets called.
Could someone please help me ?

Best Regards,
Jose
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
var url = "http://localhost:8089/AsynchWebApp/AjaxCallServlet?a=2&b=9";

Don't use an absolute URL. You should be using a server-relative URL.

I assume that the servlet is correctly declared within a package and that it is correctly mapped in the deployment descriptor?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. I see that you are already employing the good practice of Unobtrusive JavaScript -- jQuery would make all this much, much simpler.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Bear, it actually is working fine, here's my web.xml, in case you're still wondering



I hope you could tell me how to make it print invocation inside the: sum div. This is driving me crazy, I still can't figure out the problem.

Thanks in advance, and good luck !

Sincerely,

Jose


PS. By the way, I've already tried to use a server relative URL, and didn't work either. I have no idea what to do next.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what exactly doesn't work? Is the request ever sent to the server? Does the servlet try and pick it up and fail? The first thing I would do is install Firebug and find out if the request is even being sent. Then go from there. We need a bit more info on the actual problem I think.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Gregg,

I'm pretty sure the request doesn't even make it to the Servlet. it's easy to be certain of it, because the System out placed inside the doGet() method never gets printed.
Aside from that, I placed another comment in the xhtml right after the request.open() that is never reached either.

If there's any additional information that you require, please let me know, to see If I can provide it to you.

Thank you very much,

Jose.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you described doesn't mean the request is never sent, it only implies that it never makes it to the server. Install Firebug and see what happens.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, can you hit the servlet by just typing the path in the address bar? Remember, there is no difference between a standard browser made request and an ajax request as far as the server is concerned.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gregg Bolinger wrote:Also, can you hit the servlet by just typing the path in the address bar? Remember, there is no difference between a standard browser made request and an ajax request as far as the server is concerned.


Well, that little test I have already done many times, rest assured; the Servlet it's fine. May I ask you something?

Does the code look ok to you?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A word of advice: if you are not going to follow the simple (and customary) debugging steps outlined by those like Gregg, you'll find that people that can help you will start to skip your questions.

Have you verified that the HTTP request is being sent? "The servlet isn't printing" is not a meaningful debugging step. Use Firebug or a similar tool to check whether an HTTP request is being sent at all, and if so, to where and with what format.

Any other step in trying to debug this is a waste of your and everyone else's time.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I'm rehearsing and Learning Firebug as we speak, I have never used it before, so it may take a little time to understand what it does. And the fact that it's a little different than the one in the video Tutorials I'm seeing is not helping at all, (maybe it's the version or something)
I'll let you know what I found out. And don't worry, I'm all about following advice. Always,

Thank you Bear.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gentlemen, Thanks for all the waiting, I made Firebug work.

And Here's what Firebug is showing me in the console:

request.open is not a function
getSumResult()AsynchWebApp (line 39)
anonymous()AsynchWebApp (line 26)
[Break on this error] request.open("GET", url, true);\n


I hope you can tell me what steps should I follow next.

Good Luck, and thanks once more,

Jose.



 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure your request object isn't coming back null on line 14? I see it looks like you are only handling one browser for Ajax (IE uses an ActiveX Control where other browsers don't). I'd highly recommend, as Bear did, to use jQuery for this. You're entire createRequest and displaySumResult could be simplified by doing...



And that is completely cross browser supported.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid I can't take your suggestion. The reason is, I'm learning Ajax, JQuery, I can learn later. But If I intend to continue to learn from the Head First Ajax book, I better get this working.
Regarding the null request, I guess that's been taken care of by this code inside getSumResult():



right ?

Please enlighten me. I'm working with Chrome and Firefox, I never even touch Microsoft's Browser.

Thanks again,
Good Luck,

Jose
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
request = XMLHttpRequest;

should be

request = new XMLHttpRequest();
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gregg, That's awesome! It worked for Firefox. yeah it did !
I saw the response on Firebug. It still doesn't refresh the xhtml, But I can figure that out later.

Now, Could you please advice me on how to get a request object for Chrome ?

Thanks for everything.

Jose
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code:

request = new XMLHttpRequest();

will work on all real browsers. Only in IE do you need divergent code (and I believe that in IE8 they wrap the ActiveX in the above code, but I'm not 100% sure).
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

As you guys undoubtedly guessed, the problem was that, the keyword: new was missing. It's all running properly now in the browsers I'm interested in.
The good thing is, that I can take it up from here, and do the rest of the examples.

Thank you very much,

Have a nice day,

Jose.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic