java - Request parameters become null -
my code working fine while ago until classmate sent modified html file , every single parameter null. don't know happened. i'm transferring information form servlet , servlet, i'm transferring database. here's code form:
<form method="post" action="informationtransfer"> <div class='row'> <div class='col-md-6'> <label for="username">user name</label> <input type="text" class="form-control" id="username" placeholder="user name" name='username' required> </div> </div> <br /> <div class='row'> <div class='col-md-6'> <label for="lastname">last name</label> <input type="text" class="form-control" id="lastname" placeholder="last name" name='lastname' required> </div> <div class='col-md-6'> <label for="firstname">name</label> <input type="text" class="form-control" id="firstname" placeholder="first name" name='firstname' required> </div> </div> <br /> <div class='row'> <div class='col-md-6'> <label for="lastname">e-mail</label> <input type="email" class="form-control" id="email_1" placeholder="e-mail address" name='email' required> </div> <div class='col-md-6'> <label for="firstname">confirm e-mail</label> <input type="email" class="form-control" id="email_2" placeholder="confirm e-mail address" required> </div> </div> <br /> <div class='row'> <div class='col-md-6'> <label for="lastname">password</label> <input type="password" class="form-control" id="password_1" placeholder="password" name='password' required> </div> <div class='col-md-6'> <label for="firstname">confirm password</label> <input type="password" class="form-control" id="password_2" placeholder="confirm password" required> </div> </div> <br /> <div class='row'> <div class='col-md-6'> <label for='birthdate'>birthday</label> <div class="bfh-datepicker" data-max="today" data-close="false" data-date="today" data-format="y-m-d"> <input id="birthdate" type="text" data-name="birthdate" name='birthdate' style='background-color:white;'> </div> </div> <div class='col-md-6'> <label for='gender'>gender</label> <select class='form-control' name='gender'> <option>male</option> <option>female</option> </select> </div> </div> <br /> <div class='row'> <div class='col-md-4'> <label for="countries_selectors">country</label> <select id="countries_selectors" class="form-control bfh-countries" data-country="ph" name='country' required></select> </div> <div class='col-md-4'> <label for="state">state</label> <select class="form-control bfh-states" data-country="countries_selectors" id='state' name='state' required></select> </div> <div class='col-md-4'> <label for="zip">zip code</label> <input type="zip" class="form-control" id="zip" placeholder="zip code" name='zipcode' required> </div> </div> <br /> <div class='row'> <div class='col-md-12'> <label for="address">home address</label> <input type="text" class="form-control" id="address" placeholder="last name" name='address' required> </select> </div> </div> <br /> <div class='row'> <div class='col-md-10'> <label for="phone">contact number</label> <input type="text" class="form-control bfh-phone" data-country="countries_selectors" id="phone" name='phonenumber'> </div> <div class='col-md-2'> <label for="signup_b"><br /></label> <button type="submit" class="btn btn-default center-block" name='signup_b'>sign up!</button> </div> </div> </form>
here's code servlet:
@webservlet(description = "transfers contents of bean database", urlpatterns = { "/informationtransfer" }) public class transferinfo extends httpservlet { private static final long serialversionuid = 1l; protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { try { todatabase(request, response); } catch (sqlexception | classnotfoundexception e) { e.printstacktrace(); } } /** * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response) */ protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { try { todatabase(request, response); } catch (sqlexception | classnotfoundexception e) { e.printstacktrace(); } } private void todatabase(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception, sqlexception, classnotfoundexception { class.forname("com.mysql.jdbc.driver"); connection con; con = drivermanager.getconnection("jdbc:mysql://localhost:3306/foundationsystem","root","!qaz2wsx"); con.setautocommit(false); try { preparedstatement stmt1, stmt2, stmt3; stmt1 = con.preparestatement("insert addressinformation (addressid, country, zipcode, state, address) values (null,?,?,?,?)"); stmt1.setstring(1, request.getparameter("country")); stmt1.setstring(2, request.getparameter("zipcode")); stmt1.setstring(3, request.getparameter("state")); stmt1.setstring(4, request.getparameter("address")); stmt1.executeupdate(); stmt2 = con.preparestatement("insert accountdetails values (?,?,?,?,?)"); stmt2.setstring(1, request.getparameter("username")); stmt2.setstring(2, request.getparameter("password")); stmt2.setstring(3, null); stmt2.setstring(4, null); stmt2.setint(5, 3); stmt2.executeupdate(); stmt3 = con.preparestatement("insert personalinformation values (?,?,?,?,?,?,?,?)"); statement address = con.createstatement(); resultset result = address.executequery("select max(addressid) addressinformation"); result.next(); stmt3.setstring(1, request.getparameter("username")); stmt3.setstring(2, request.getparameter("lastname")); stmt3.setstring(3, request.getparameter("firstname")); stmt3.setstring(4, request.getparameter("gender")); stmt3.setstring(5, request.getparameter("birthdate")); stmt3.setint(6, result.getint(1)); stmt3.setstring(7, request.getparameter("email")); stmt3.setstring(8, request.getparameter("phonenumber")); stmt3.executeupdate(); con.commit(); //response.sendredirect(); } catch (exception e) { con.rollback(); system.out.println(e); } } }
take care of case here. eg.
<input type="zip" class="form-control" id="zip" placeholder="zip code" name='zipcode' required>
you data
stmt1.setstring(2, request.getparameter("zipcode"));
and not
stmt1.setstring(2, request.getparameter("zipcode")); // c capital
name
of input
tag in form should match parameter name use in getparameter()
method.
Comments
Post a Comment