我有一個數據庫,裏面有一些用戶,它們有一個用戶名和一個密碼。 如果來自用戶的輸入與我希望它重定向到主頁的數據庫中的輸入相同,我想在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("");
}
}
}
請張貼控制器類也。 – pikrut