jsp - use Ajax to call data from servlet -
i know question has been asked before, can't seem find awnser on it.
i'm trying call servlet method when page loaded, @ moment it's endless loop cause every time servlet posts response page gets reloaded , calls servlet again calls reloads page again etc etc..
i'm collecting id url use in servlet collect data need. here code i'm using.
<form style="display:none;" action="servletc" method="post"> <input type="hidden" name="hdn_parameter" value="<%= request.getparameter("productid")%>"/> <input type="submit" id="btn_loadform" name="loadform" style="display: none;"/> <script> document.getelementbyid('btn_loadform').click(); </script> </form>
and got couple pieces of code throughout page
<img id="picture_img" class="" style="width:300px;" src="${imageresponse}" />
the following code on servlet
if (request.getparameter("loadform") != null) { int productid = integer.parseint(request.getparameter("hdn_parameter")); string link = "picturedetail.jsp?productid=" + integer.tostring(productid); product p = null; try { p = dbconnect.getproduct(productid); } catch (sqlexception ex) { logger.getlogger(servletc.class.getname()).log(level.severe, null, ex); } request.setattribute("imageresponse", p.getpath()); request.setattribute("titleresponse", p.getname()); request.setattribute("descriptionresponse", p.getdescription()); request.setattribute("priceresponse", p.getprice()); loaded = true; requestdispatcher dis = request.getrequestdispatcher(link); dis.forward(request, response); }
could me on should make happen without refreshing page work smoothly.
thanks in advance.
the request goes out on click of button proper http request , response naturally repaint page. have form ajax request on button click, parse response on success , on page (with parsed response data).
Comments
Post a Comment