wood burning stoves*
The moose likes HTML, CSS and JavaScript and the fly likes Ajax and Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Ajax and Servlets" Watch "Ajax and Servlets" New topic
Author

Ajax and Servlets

rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
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


Cheers!
RSR
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Mike Firkser
Ranch Hand

Joined: Oct 21, 2003
Posts: 247

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.


Mike Firkser
Rutgers '84
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

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 .


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ajax and Servlets