2012-09-27 47 views
-1

你好,我是新來JSP。我正在開發一個project.I客戶註冊頁面希望我的用戶ID的unique.So我發送的ID驗證的的onblur事件它的textfield.But我得到一個空指針exception.Please幫我這個代碼。JSP用戶ID驗證

HTML代碼(register.html):

<head> 

<script type="text/javascript"> 
function OnButton1() 
{ 
var userid = document.getElementById("user").value; 
window.location.replace("register.jsp?userid="+userid); 
document.registerform.action = "useridvalidcode.jsp" 

    document.registerform.submit(); 
    return true; 
} 
    function confirmPass() { 
     var pass = document.getElementById("pass").value; 
     var confPass = document.getElementById("c_pass").value; 
     if(pass != confPass) {   
alert('Wrong confirm password !'); 
document.getElementById("c_pass").value=""; 
document.registerform.cpassword.focus(); 

} }</script> 
</head> 

<body bgcolor="#DFBB81" onload="document.registerform.userid.focus();"> 
<div align="center"> 
    <table width="1024" border="0"> 
    <tr> 
     <td><jsp:include page="head.jsp"/></td> 
    </tr> 
    <tr> 
     <td height="217" valign="top"> 
     <form name="registerform" method="post" action="registercode.jsp"> 
     <table width="563" border="0"> 
     <tr> 
      <td width="19">&nbsp;</td> 
      <td width="144"><font color="#990000" size="+2">Register:</font></td> 
      <td width="204">&nbsp;</td> 
      <td width="168">&nbsp;</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td>User Id: </td> 
      <td><label></label></td> 
      <td>&nbsp;</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td><input type="text" name="userid" id="user" required="required" onblur="return OnButton1();"/></td> 
      <td><label></label></td> 
      <td>&nbsp;</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td>Password: </td> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td><input type="password" id="pass" name="password" required="required" /></td> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td>Confirm password: </td> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td><label> 
     <input type="password" id="c_pass" value="" onblur="confirmPass()" name="cpassword" required="required" /> 
      </label></td> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td>Email:</td> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td><label> 
      <input type="email" name="email" required="required" /> 
      </label></td> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td>Mobile no: </td> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td> 
      <input type="tel" pattern='\d{10}' title="10 digit mobile number format" required="required" name="mobileno"/>   </td> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td>Address:</td> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td><textarea name="address" rows="5" required="required" style="resize:none"></textarea></td> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td><input type="submit" name="Submit" value="Submit" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      <input type="reset" name="Submit2" value="Reset" /></td> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td><span class="style2">*</span> All fields mandatory </td> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
     </tr> 
     </table> 
     </form></td> 
    </tr> 
    <tr> 
     <td><jsp:include page="foot.html"/></td> 
    </tr> 
    </table> 
</div> 
</body> 
</html> 

JSP代碼(useridvalidcode.jsp):

<body> 
<%@page import="java.sql.*"%> 
<% 
String userid=request.getParameter("userid"); 
try{ 
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
Connection con = DriverManager.getConnection("jdbc:odbc:pd"); 
ResultSet rs = null; 

Statement st = con.createStatement(); 
String sql=("select * from register where userid='"+ userid +"'"); 
st.executeQuery(sql) ; 
       int ct = 0; 
    while(rs.next()) 
     { 
     ct++; 
     } 
    if (ct > 0) 
     { 
     %><script type="text/javascript"> alert("duplicate"); </script> <% 
     } 
    else 
     { 
    %><script type="text/javascript"> alert("new"); </script> <% 
     } 
rs.close(); 
st.close(); 
con.close(); 
} 
catch(Exception e){ 
out.println(e.toString()); 
       return ; 

} 
%> 
<script language="javascript"> 
alert("Successfully regestered"); 
</script> 
<jsp:include page="../page/home.jsp"/> 
</body> 
</html> 
<body> 
<%@page import="java.sql.*"%> 
<% 
String userid=request.getParameter("userid"); 
try{ 
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
Connection con = DriverManager.getConnection("jdbc:odbc:pd"); 
ResultSet rs = null; 

Statement st = con.createStatement(); 
String sql=("select * from register where userid='"+ userid +"'"); 
st.executeQuery(sql) ; 
       int ct = 0; 
    while(rs.next()) 
     { 
     ct++; 
     } 
    if (ct > 0) 
     { 
     %><script type="text/javascript"> alert("duplicate"); </script> <% 
     } 
    else 
     { 
    %><script type="text/javascript"> alert("new"); </script> <% 
     } 
rs.close(); 
st.close(); 
con.close(); 
} 
catch(Exception e){ 
out.println(e.toString()); 
       return ; 

} 
%> 
<script language="javascript"> 
alert("Successfully regestered"); 
</script> 
<jsp:include page="../page/home.jsp"/> 
</body> 
</html> 

<body> 
<%@page import="java.sql.*"%> 
<% 
String userid=request.getParameter("userid"); 
try{ 
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
Connection con = DriverManager.getConnection("jdbc:odbc:pd"); 
ResultSet rs = null; 

Statement st = con.createStatement(); 
String sql=("select * from register where userid='"+ userid +"'"); 
st.executeQuery(sql) ; 
       int ct = 0; 
    while(rs.next()) 
     { 
     ct++; 
     } 
    if (ct > 0) 
     { 
     %><script type="text/javascript"> alert("duplicate"); </script> <% 
     } 
    else 
     { 
    %><script type="text/javascript"> alert("new"); </script> <% 
     } 
rs.close(); 
st.close(); 
con.close(); 
} 
catch(Exception e){ 
out.println(e.toString()); 
       return ; 

} 
%> 
<script language="javascript"> 
alert("Successfully regestered"); 
</script> 
<jsp:include page="../page/home.jsp"/> 
</body> 
</html> 
+1

發佈異常堆棧跟蹤更有幫助。 – turtledove

回答

2

我相信錯誤是從這裏來的。

ResultSet rs = null; 
... 
while(rs.next()) 

其中rs始終爲空。

+0

+1這就是他們將數據庫內容放入JSP時的情況...... –

+0

感謝您的建議,但是您能否爲我提供此代碼的解決方案。 –

+0

將st.executeQuery(sql)更改爲rs = st.executeQuery(sql);我建議總是檢查空(如果(rs!= null)while(rs.next()))... – Alex