需要帮助才能理解为什么要求。 getParameter返回null

时间:2017-03-22 23:03:50

标签: javascript jsp

request.getParameter返回null。但是我看到document.getElementById捕获了值,但是没有通过request.getParameter捕获。不知道为什么?

<script type="text/javascript">

           function checkExists(){

             var NUSN=document.getElementById('UName').value;
                       //  alert("helloeeee ");
                      // alert (" NUSN is : " + NUSN);
            <% 
            Connection conn = null;
            String url= "jdbc:sqlserver://localhost:1433;databaseName=Movie";
            String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
            String username ="sam";
            Class.forName(driver);           
            conn=java.sql.DriverManager.getConnection(url,username,"sa");
            String NUSN1 = request.getParameter("UName");
            System.out.println("New user is : ");
            System.out.println(NUSN1);   //here the value is null- dont know why
            Statement stmt = conn.createStatement();
           ResultSet rs=stmt.executeQuery("select username from Userdetails where usename in ("+ NUSN1+")");
            while (rs.next()){
                if (rs.first()){
                    out.println("User name already taken");

                }
                else out.println("User name Valid");
            }



            %>
}
</script>

这是我的表格:仅发布相关部分

    <tr> <td><label for="User Name">USER NAME:</label></td><br><br>
    <td><input type="text" name="UName" value="" size="10" id="UName" /></td>
     </tr>

1 个答案:

答案 0 :(得分:0)

我猜你在表单提交时返回false,这就是为什么是null! 看我的例子:

<!DOCTYPE html>
<%
    if (request.getParameter("code") != null)   
        System.out.println(" AFTER SUBMIT code: "+request.getParameter("code"));
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test Form</title>
</head>
<body>
<form method="post" onSubmit="return validateForm()">
Code:<input type="text" name="code" id="code"/>
<button type="submit">Submit</button>
</form>
<script>
function validateForm(){
    console.log(document.querySelector("#code"));

    <%
        System.out.println("CODE: "+request.getParameter("code"));
    %>
}
</script>
</body>
</html>