2016-11-29 60 views
0

我有一個數據庫,裏面有一些用戶,它們有一個用戶名和一個密碼。 如果來自用戶的輸入與我希望它重定向到主頁的數據庫中的輸入相同,我想在JSP中創建一個包含2個字段「login」和「password」的登錄表單。 我知道如何做到這一點在PHP中,但即時通訊全新的jsp,所以我真的不知道從哪裏開始,我認爲JavaScript將需要完成這一點。 我已經看到一些類似的問題,但沒有答案似乎真的爲我工作。JSP表單登錄檢查器

這裏是我的JSP頁面:

<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <link rel="stylesheet" href="<c:url value="/resources/css/style.css"/>"> 
    <title>login</title> 
</head> 
<body> 
    <div class="login-block"> 
    <h1>Login Page</h1> 
    <div class="inlog-block"> 
    <form action="" method="post"> 
     <p>Enter Username: </p> 
     <input type="text" name="username" required="required" /> <br> 
     <p>Enter password: </p> 
     <input type="password" name="password" required="required"/><br><br> 
     <input type="submit" name="loginbutton" value="Login"/> 
    </form> 
    </div> 
    </div> 
</body> 
</html> 

而且我已經能夠讓我的用戶從數據庫中一個ArrayList在我的模型,也許我需要在我的JSP使用呢?

我EmployeeList的類:

public class EmployeeList { 
    private ArrayList<Employee> employees = new ArrayList<>(); 

public EmployeeList() 
{ 
    loadEmployees(); 
} 

public void loadEmployees(){ 
    DAOEmployee DAOE = new DAOEmployee(); 
    employees = DAOE.LoadAllEmployees(); 
} 

public ArrayList<Employee> getEmployees() { 
    return employees; 
} 

public void setEmployees(ArrayList<Employee> employees) { 
    this.employees = employees; 
} 

} 

我DAOEmployee類:

public class DAOEmployee extends DAObject{ 
public ArrayList<Employee> LoadAllEmployees(){ 

    String sql = "SELECT * FROM EMPLOYEE"; 
    Employee e = null; 
    ArrayList<Employee> employees = new ArrayList<>(); 

    try { 
     ResultSet rs; 
     Statement stmt = openConnection().createStatement(); 
     rs = stmt.executeQuery(sql); 

     while(rs.next()){ 

      int ide = rs.getInt("id"); 
      String first = rs.getString(2); 
      String last = rs.getString(3); 
      String mail = rs.getString(4); 
      String adres = rs.getString(5); 
      String zip = rs.getString(6); 
      String funct = rs.getString(7); 
      String user = rs.getString(8); 
      String pass = rs.getString(9); 

      e = new Employee(ide,first, last, mail, adres, zip, funct, user, pass); 


      employees.add(e); 

     } 
    } 
     catch (SQLException x) { 
      // TODO Auto-generated catch block 
      x.printStackTrace(); 
     } 


    return employees; 

} 
} 

而且我DAObject類:

public class DAObject { 
private String url = "jdbc:mysql://127.0.0.1/mydatabase"; 
private String user = "root"; 
private String password = "root"; 

public Connection conn = null; 

public DAObject(){ 

} 

public Connection openConnection(){ 
    try{ 
     conn = DriverManager.getConnection(url, user, password); 
     System.out.println("Connection succesfull"); 
    } 
    catch(SQLException e){ 
     e.printStackTrace(); 

    } 

    return conn; 
} 

public void CloseConnection(){ 
    try{ 
     conn.close(); 
    } 
    catch(SQLException e){ 
     e.printStackTrace(); 
     System.out.println(""); 
       } 
} 
} 
+0

請張貼控制器類也。 – pikrut

回答

0

您需要一種方法來搜索與一個especific用戶不同的SQL指令,並且你返回一個對象Employee或布爾值w/e:

String sql = "SELECT * FROM EMPLOYEE WHERE USER=? AND PASS=?"; 

在您的servlet中,您可以調用它並使用表單中的輸入值檢查此用戶是否存在註冊表。

String userName = req.getParameter("username"); 

編輯:

a)您需要一種方法來搜索該用戶,這樣的事情(DAO層爲例):

public User search(String userName, String password, Connection con) throws Exception{ 
    User user = null; 

    String sql = "SELECT USERNAME, PASSWORD FROM T_USERS WHERE USERNAME=? AND PASSWORD=?"; 

    PreparedStatement pStmt = con.prepareStatement(sql); 
    pStmt.setString(1, userName); 
    pStmt.setString(2, password); 
    ResultSet rS = pStmt.executeQuery(); 

    if(rS.next()){ 
     user = new User(rS.getString("USERNAME"), rS.getString("PASSWORD")); 
    } 

    rS.close(); 
    pStmt.close(); 

    return user; 
} 

二)在您Servlet的你檢查它doPost方法:

String user = req.getParameter("username"); 
String pass = req.getParameter("password"); 

Connection con = null; 

try { 
    con = ConexaoFactory.getInst().getConnection("", "");  

    User user = BOClass.search(user, pass, con); 

    if(user == null){ 
     //not found; 
    }else{ 
     //found; 
    } 
} catch (Exception e) {   
    e.printStackTrace(); 
}finally {   
    try {    
     if(con != null) 
      con.close(); 
    } catch (Exception e2) { 
     e2.printStackTrace(); 
    } 
} 

這只是一個例子,你如何做到這一點。

+1

這看起來像正確的方式,但我從來沒有使用過servlet,所以你有更大的例子嗎? –

+0

看我的編輯例如。 –