java - Posting to self servlet creating an infinite loop -
i trying develop servlet page display form , user fill in form , post servlet (problem). pasted code below. when call servlet page displayed ok , can fill in form when click submit , post it, programs enters loop mysql insert command runs indefinitely , forced stop program.
the problem seems coming line:
request.getrequestdispatcher("/problem").include(request, response);
however way see call servlet again after post page somehow creating program loop. idea why case?
below servlet:
@webservlet(description = "problem page", urlpatterns = { "/problem" }) public class problempage extends httpservlet { private static final long serialversionuid = 1l; /** * @see httpservlet#httpservlet() */ public problempage() { super(); // todo auto-generated constructor stub } protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub string problemid = request.getparameter("pbid"); string option = request.getparameter("opt"); string productid = request.getparameter("pdid"); if(problemid==null && option==null && productid==null) { printwriter out = response.getwriter(); try { for(string printline:(new problem()).printproblemadd()) { out.print(printline); } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } else if(problemid!=null && option!=null && productid==null) { if(option.equals("edit")) { request.getrequestdispatcher("problem").include(request, response); } else if(option.equals("del")) { } else { printwriter out = response.getwriter(); out.print("<p>page invalid.</p>"); } } else if(problemid==null && option==null && productid!=null) { //lists problems product problem problem = new problem(productid); printwriter out = response.getwriter(); try { for(string printline:problem.print()) { out.print(printline); } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } else { //page invalid printwriter out = response.getwriter(); out.print("<p>page invalid.</p>"); } } protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub string url = "jdbc:mysql://localhost:3306/"; string dbname = "knowledge"; string driver = "com.mysql.jdbc.driver"; string username = "root"; string password = ""; connection conn = null; try { class.forname(driver).newinstance(); conn = (connection) drivermanager.getconnection(url + dbname,username,password); //st = (statement) conn.createstatement(); string mysqlquery = "insert problems (id,productid,description) values (?,?,?);"; preparedstatement updatestatement = (preparedstatement) conn.preparestatement(mysqlquery); updatestatement.setint(1,0); updatestatement.setstring(2, request.getparameter("productid")); updatestatement.setstring(3, request.getparameter("description")); int res = updatestatement.executeupdate(); request.setattribute("message", res>0?"record added: " + request.getparameter("description"):"record failed"); conn.close(); } catch (exception e) { e.printstacktrace(); } { try { conn.close(); } catch(exception e) { e.printstacktrace(); } } request.getrequestdispatcher("/problem").include(request, response); } }
looks final line trying call same handler same request, causing loop? oracle docs
Comments
Post a Comment