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!
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 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().