let's say I have this code in javascript:
function doAnAjaxCall () { var xhr1 = new XMLHttpRequest(); xhr1.open('GET', '/mylink', true); xhr1.onreadystatechange = function() { if (this.readyState == 4 && this.status==200) { alert("Hey! I got a response!"); } }; xhr1.send(null); }
and let the code in the servlet:
public class RootServlet extends HttpServlet { public void doGet (HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.getWriter().write("What up doc?"); resp.setStatus(200); } }
Will xhr1 wait for new readistate changes? Or is he closed as soon as he receives the first answer? If it remains open, will it lead to a memory leak / slow browser after some time and some of them accumulate? Should I always call resp.getWriter (). close () at the end of the servlet code?
And finally, for jQuery fans:
Does $.ajax() like XMLHttpRequest() in this regard?
java javascript jquery ajax servlets
Aleadam
source share