Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Feeling like an idiot with XMLHttpRequest - Servlet talking

 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I must preface this by saying Im feeling like a first day programmer here ... like an idiot for even to have to ask this question but I am stumped!

Im spending the day learning XMLHttpRequest and have seen mutliple posts and articles around saying the XMLHttpRequest object simply calls a servlet, grabs the data and displays it... seemed simple but Im stuck wondering HOW the Servlet returns the data! Ive been thru the servlet API 3 times looking for something, some method of the Response object that allows me to send the data back and either Im completely missing something here or there is some layer, some piece of the puzzle I am missing...

Can someone kick me in the head hear and send me down the right path!

Thanks in advance!
 
Bryan Hughes
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on this as well. I've got a working example at work. I'll reply to this message tomorrow when I get a moment to review it.
 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Id actually like to see it ...... I was able to get the data back (im pulling data from a db) by using PrintWriter but MAN that seems weird sending it back that way! Maybe Im making too much of it but it just doesnt seem like the right way to do it!

Thanks Again!
 
H Wilson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by DC Dalton:
Thanks, Id actually like to see it ...... I was able to get the data back (im pulling data from a db) by using PrintWriter but MAN that seems weird sending it back that way! Maybe Im making too much of it but it just doesnt seem like the right way to do it!

Thanks Again!


You can also redirect or forward to a jsp. You can actually forward or redirect to just about any url, but I'm guessing that since you have data to display, you might want to store the data in a bean and display it in a jsp that has access to that bean. Check out the javadocs on javax.servlet.RequestDispatcher for the forward() method and javax.servlet.http.ServletResponse for the sendRedirect() method. I think Marty Hall's book "Core Java Servlets and Java ServerPages" may contain a good explanation of these methods (can't remember offhand as I read it some time ago) and I'm sure that "Head First Servlets and JSP" does. You want to call one of these methods at the end of your doGet() or doPost().
 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not looking to do that at all ..... Im just sending the data back to the calling XMLHttpRequest object, which I was able to do with a simple out.print

thanks
 
Juan Smith
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote this little app that talks to a servlet and the servlet talks to the google java API to do a background search. I think it's exactly the kind of example you are looking for - The source code is available:

http://www.josesandoval.com/2005/03/running-xmlhttprequest-with-java.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic