简体   繁体   English

为什么request.getParameter()方法返回null?

[英]Why does request.getParameter() method returns null?

I have a static HTML document that posts a form to a Java servlet. 我有一个静态HTML文档,该文档将表单发布到Java servlet。 The servlet then takes the values of the form and forwards them to a SQL database. 然后,该Servlet接受表单的值并将其转发到SQL数据库。 However, the problem is that the database claims a value to be null when I am certain that the value is not null. 但是,问题在于,当我确定该值不为空时,数据库声称该值为空。

Here is the form: 形式如下:

<form method="post" action="Handler" target="_blank" enctype="multipart/form-data">
  <div class="row uniform 50%">
    <div class="6u 12u(mobilep)">
      <input type="text" name="name" id="name" placeholder="Username" />
    </div>
    <div class="6u 12u(mobilep)">
      <input type="email" name="email" id="email" placeholder="Email" />
    </div>
  </div>
  <div class="row uniform 50%">
    <div class="6u 12u(mobilep)">
      <input type="password" name="password" id="password" placeholder="Password" />
    </div>
  </div>
  <div class="row uniform 50%">
    <div class="12u">
      <textarea name="bio" id="bio" placeholder="Describe yourself" maxlength="200" rows="3"></textarea>
    </div>
  </div>
  <div class="row uniform">
    <div class="12u">
      <ul class="actions align-center">
        <li><input type="submit" value="Send Message" /></li>
      </ul>
    </div>
  </div>
</form>

Here is the servlet code: 这是servlet代码:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    Connection c = null;
    PrintWriter op = response.getWriter();
    response.setContentType("text/html");

    try{
        Class.forName("com.mysql.jdbc.Driver");
        c = DriverManager.getConnection(DB_URL, "root", "root");
    }catch(Exception s){
        op.println("<html>");
        op.println("<body><h1><strong>"+ "Error: "+ s +"</strong></h1></body>");
        op.println("</html>");
    }

    String username = request.getParameter("name");
    String email = request.getParameter("email");
    String pass = request.getParameter("password");
    String bio = request.getParameter("bio");
    String proPicName = "false";
    Long time = System.currentTimeMillis();
    String sysTime = time.toString();

    Statement stmt = null;

    try{
        stmt = c.createStatement();
        stmt.execute("USE dvlpr;");
        stmt.execute("INSERT INTO user_tbl (username, email, password, bio, pro_pic, last_on, date_created)" + " VALUES ("+username+", "+email+", "+pass+", "+bio+", "+proPicName+", "+sysTime+", "+sysTime+");");


        op.println("<html>");
        op.println("<body><h1><strong>Connection made! Username: " + username+ " Email: " + email+ " Your account has been created. We'll keep your password private, too. Thanks!</strong></h1>");
        op.println("</body>");
        op.println("</html>");
    }catch(Exception s){
        op.println("<html>");
        op.println("<body><h1><strong>"+ "Error: "+ s +"</strong></h1></body>");
        op.println("</html>");
    }

}

The error code is below: 错误代码如下:

Error:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'username' cannot be null 错误:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:列'username'不能为null

Why is this returning null? 为什么返回的是null?

The parameters are fine. 参数很好。 The query isn't being executed properly because of syntax. 由于语法原因,查询未正确执行。 The values themselves need to be surrounded by quotes so it should look like: 值本身需要用引号引起来,因此应类似于:

VALUES('"+username+"',....

As you can see I added one ' before the double quotation mark and one after so the ' will be part of the resulting String. 如您所见,我在双引号之前添加了一个' ,在双引号之后添加了一个,因此'将成为结果字符串的一部分。 Just like in any other SQL insert you would do VALUES('MyUsername', 'MyPassword',...); 就像在其他任何SQL插入中一样,您将执行VALUES('MyUsername', 'MyPassword',...); .

Also you might want to use one execute method instead of 2 so it would be: 另外,您可能希望使用一种execute方法而不是2种,因此它将是:

stmt.execute("USE dvlpr; INSERT INTO....");

And also there is no need for the + before " VALUES 而且也不需要在" VALUES前加上+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM