我的JSP if-else语句无法正常工作

时间:2019-02-09 14:28:54

标签: java html jsp

我是JSP的新手,我想使用JSP验证HTML表单,我在代码中使用了if-else语句。但这不能正常工作。

<%
        String fname = request.getParameter("fname");
        String lname = request.getParameter("lname");
        String email = request.getParameter("email");
        String uname = request.getParameter("username");
        String pw = request.getParameter("password");
        String pw1 = request.getParameter("confirm");
        String phone = request.getParameter("phone");
        String idno = request.getParameter("idno");
        String gender = request.getParameter("gender");
        String dob = request.getParameter("dob");
        String bgroup = request.getParameter("bloodgroup");
        String bweight = request.getParameter("weight");
        String lastdonate = request.getParameter("lddate");
    if (request.getParameter(fname) == null
            || request.getParameter(lname) == null 
            || request.getParameter(email) == null
            || request.getParameter(uname) == null
            || request.getParameter(pw) == null
            || request.getParameter(pw1) == null
            || request.getParameter(phone) == null
            || request.getParameter(idno) == null
            || request.getParameter(dob) == null 
            || request.getParameter(bweight) == null
            || request.getParameter(lastdonate) == null) {
        out.println("<div class='alert-message alert-message-warning' align='center'>"+
            "<h4>Alert Message Warning</h4>"+
            "<p>Some Fields Are Empty <br>" +
                "<strong>Please Fill All The Fields</strong>."+
            "</p></div>");
    } 
   else if (request.getParameter(pw) != request.getParameter(pw1) ) {
        out.println("<div class='alert-message alert-message-warning' align='center'>"+
            "<h4>Alert Message Warning</h4>"+
            "<p>Confirmed Password Doesn't Match With the Password <br>" +
                "<strong>Please Re-Type Your Password</strong>."+
            "</p></div>");
    }

    else {

        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (Exception ex) {
            System.out.println(ex);
        }
        try {
           Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/minutehelp","root","");
           Statement stm = con.createStatement();
           stm.executeUpdate("INSERT INTO donors VALUES(NULL, '"+fname+"', '"+lname+"', '"+email+"', '"+uname+"', '"+pw1+"', '"+phone+"', '"+idno+"', '"+gender+"', '"+dob+"', '"+bgroup+"', '"+bweight+"', '"+lastdonate+"')");
           stm.close();
        } catch (Exception ex) {
            out.println(ex);
        } 

    }

        %>

首先,我想检查所有字段是否为空或已填充,然后我想检查密码和确认的密码是否相同,如果没有错误,我想将这些数据发送到数据库表“ donors” '。但是它无法正常工作,并且始终在if语句内显示警告消息,并且未插入数据。但是,如果我仅使用if语句而不是if-else,则即使条件为false,它始终显示第一条警告消息并每次都插入数据。 我是JSP的新手,请有人在这里帮助我。 谢谢。

1 个答案:

答案 0 :(得分:0)

if语句中的问题是您尝试比较request.getParameter(fname)等。但这不存在。如果要比较,请在您的request.getParameter("something")

中放入“”

此外,如果您想在案例fname,pw,pw1等中使用变量进行比较,请尝试执行以下操作

 if(fname==null || uname==null ... ){
     //do something
     }

现在要查看密码值是否相等,请尝试在 else-if

中执行此操作
else if (pw.equals(pw1) ) {
  //do something
}

此外,您可以像这样修改代码以实现所需的功能

if(fname!=null || uname!=null ...){

    if (pw.equals(pw1) ) {
  //your insert code put here 
    }else{
     out.println("<div class='alert-message alert-message-warning' align='center'>"+
        "<h4>Alert Message Warning</h4>"+
        "<p>Confirmed Password Doesn't Match With the Password <br>" +
            "<strong>Please Re-Type Your Password</strong>."+
        "</p></div>");
  }


}else{

  out.println("<div class='alert-message alert-message-warning' align='center'>"+
            "<h4>Alert Message Warning</h4>"+
            "<p>Some Fields Are Empty <br>" +
                "<strong>Please Fill All The Fields</strong>."+
            "</p></div>");


}

希望这会有所帮助!