• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Ajax and Servlets

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 249
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic