2014-07-03 34 views
0

我想验证数据库中数据存储的用户名和密码。我在开发中使用Netbeans IDE。我遇到的问题是它保持显示一个错误页面,内容为内部服务器错误。有人能帮我检查我的代码吗?我有一些像这样的代码:jsp用数据库用户登录验证

loginForm.jsp中:

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Login Form</title> 
    </head> 
    <body bgcolor="#D8CEF6"> 
     <h1>Login Page</h1> 
     <fieldset> 
      <center> 
     <legend><h2>Sign in Details</h2> </legend> 
     <form action="LoginCheck.jsp" method="post"> 

      <br/>Username:<input type="text" name="username"/> 
      <br/>Password:<input type="password" name="password"/> 
      <br/> 
      <tr> 
      <td>usertype</td> 
      <td> 
       <select name="usertype"> 
        <option value="student">student</option> 
        <option value="faculty">faculty</option> 
       </select> 
      </td> 
      </tr> 
      <tr> 
       <td> 
      <br/><input type="submit" value="Submit" name="bt"/> 
       </td> 
      </tr> 
     </form> 
    </center> 
       </fieldset> 
     </body> 
     </html> 

LoginCheck.jsp:

<%@page import ="java.sql.*" %> 
<%@page import ="java.io.IOException" %> 
<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@page import="java.io.*"%> 
    <html> 
     <head> 
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
      <title>Login Check</title> 
     </head> 
     <body> 
      <% 
      String username = request.getParameter("username"); 
      String password = request.getParameter("password"); 
      String userType = request.getParameter("usertype"); 
      String dbURL = "jdbc:derby://localhost:1527/learningApp"; 
      String dbuser = "learningApp"; 
      String dbpassword = "[email protected]"; 
      Connection theConnection = null; 
      PreparedStatement theStatement = theConnection.prepareStatement("select * from USERNAME where username=? and password=?"); 
      try{ 
      Class.forName("org.apache.derby.jdbc.ClientDriver"); 
      }catch(Exception ex){ 
      System.out.println("Class Not Found"); 
      } 
      try{ 
      theConnection = DriverManager.getConnection(dbURL, dbuser, dbpassword); 
      }catch(Exception ex){ 
      System.out.println("Connection Error"); 
      } 
      try{ 
       theStatement.setString(1,request.getParameter("username")); 
       theStatement.setString(2,request.getParameter("password")); 
       ResultSet theResult = theStatement.executeQuery(); 
       if(theResult.next()){ 
      response.sendRedirect("Home.jsp"); 
     } 

      }catch(Exception ex){ 
      System.out.println("Statement Error"); 
      } 


      %> 
    </body> 
    </html> 

我的数据库名是learningApp用口令P @ ssw0rd 并有一个表名为USERNAME与用户(用户名=爱丽丝,密码=爱丽丝)

+0

只从代码中很难弄清楚你正在运行什么。如果你得到一个错误页面,可能是由于抛出了一些异常。也许你可以用你的stacktrace扩展这个帖子? – Jeroen

回答

0

您可能需要将derbyclient.jarderby.jar置于WebContent/WEB-INF/Lib,并且在构建路径中也包含相同的内容。

更新:

我改变你的代码(LoginCheck.jsp)这一点,(我使用Oracle数据库)。似乎对我来说工作得很好。

<body> 
     <% 
     String username = request.getParameter("username"); 
     String password = request.getParameter("password"); 
     String userType = request.getParameter("usertype"); 
     String driver = "oracle.jdbc.driver.OracleDriver"; 
     String dbURL = "jdbc:oracle:thin:@10.113.130.22:1521:ORCL"; 
     String dbuser = "user"; 
     String dbpassword = "password"; 
     Connection theConnection = null; 
     PreparedStatement theStatement = null; 


      try{ 
       Class.forName(driver); 
       theConnection=DriverManager.getConnection(dbURL,dbuser,dbpassword); 
       theStatement = theConnection.prepareStatement("select * from USERNAME where username=? and password=?"); 
       theStatement.setString(1,request.getParameter("username")); 
       theStatement.setString(2,request.getParameter("password")); 
       ResultSet theResult = theStatement.executeQuery(); 

       if(theResult.next()) 
        System.out.println("Success"); 
       else 
        System.out.println("Failed"); 

       }catch(Exception e){ 
        System.out.println("Exception occured! "+e.getMessage()+" "+e.getStackTrace()); 
       } 
     %> 
</body> 
+0

它的工作。非常感谢。 –