• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ajax and Servlets

 
rakesh sugirtharaj
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I m new to Ajax. I m trying to print some string in a text field which i get from my servlet.

response.getWriter().write("sample output");

I have set the content type as text.

In my javascript i ve the following code

ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.myForm.time.value = ajaxRequest.responseText;
}
}
ajaxRequest.open("GET", "http://localhost:8080/SampleServletProject/my.do", true);
ajaxRequest.send(null);

i ve a text field in my html that calls this method.

I m able to get the text output when i directly run the servlet in my mozilla. But not in the text field that i want. What is the error here?
Should i include any request header in my XMLHttpRequest object?

Thanks in advance
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the servlet is working when you call it directly from a servlet then the problem is more likely on the client side.

Moving to our HTML/Javascript forum where client side code is normally discussed.
 
Mike Firkser
Ranch Hand
Posts: 249
Java Oracle PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing I quickly saw is you need to add a nested if in there:



That checks to make sure you got a good response. I also also added some alerts in there that may help troubleshoot.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rakesh sugirtharaj, please be sure to use UBB code tags when posting code to the forums. Unformatted code is extermely hard to read and most people will just go elsewhere. Please read this for more information.

You can go back and change your post to add code tags by clicking the .
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the subject of this week's book promotion is jQuery, I'll point out that jQuery's Ajax handling is top-notch and handles a lot of these nuances for you. When using "raw" Ajax there are so many nuances that could go wrong that at this stage of the game, it makes little sense to not use a library that has already worked these kinks out for you.

I haven't written "raw" Ajax code in years -- first I employed Prototype for my Ajax needs, and now I've switched to jQuery.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic