请求参数变为null

时间:2015-04-25 17:42:38

标签: java mysql servlets

我的代码在前一段时间内工作得非常好,直到我的同学发送了一个修改过的HTML文件,并且每个参数现在都为null。我真的不知道发生了什么。我正在将信息从表单传输到servlet和servlet,我将它传输到数据库。以下是表单的代码:

                            <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>

这是servlet的代码:

@WebServlet(description = "transfers contents of bean to 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 INTO 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 INTO 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 INTO PersonalInformation VALUES (?,?,?,?,?,?,?,?)");
            Statement address = con.createStatement();
            ResultSet result = address.executeQuery("SELECT max(AddressID) FROM 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);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

在这里处理案件。例如。

public class Hotel
{
   public int Id { get; set; }
   public string Name { get; set; }
   ...
}

你可以通过

获得这些数据
<input type="zip" class="form-control" id="zip" placeholder="Zip Code" name='zipcode' required>

而不是

stmt1.setString(2, request.getParameter("zipcode"));
表单中stmt1.setString(2, request.getParameter("zipCode")); // C is capital 标记的

name应与您在input方法中使用的参数名称匹配。

答案 1 :(得分:0)

似乎是名称案例问题。匹配html文件中输入字段的参数名称和servlet中的request.getParameter("fieldName")