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!
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.
Joined: May 28, 2001
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!
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!
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 JavaServlets 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().
Joined: May 28, 2001
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
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: