我想将数据库中的数据值放入
Employee
类中,然后在JSP文件中显示所有值。但是,继续出现错误,提示列表为空。
package employee;
import java.util.Date;
public class Employee {
private int id;
private String firstName;
private String lastName;
private String email;
private Date hireDate;
public Employee() {
}
public Employee(int id, String firstName, String lastName, String email, Date hireDate) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.hireDate = hireDate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getHireDate() {
return hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
", hireDate=" + hireDate +
'}';
}
}
package employee;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
@WebServlet(urlPatterns="/employees", loadOnStartup = 1)
public class EmployeeServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Employee> employees = new ArrayList<>();
String url = "jdbc:mariadb://127.0.0.1:3306/test03";
String user = "root";
String password = "1234";
try {
Class.forName("org.mariadb.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new ServletException("JDBC Driver not found.");
}
try (Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id, first_name, last_name, email, hire_date FROM employees")) {
while (rs.next()) {
Employee employee = new Employee();
employee.setId(rs.getInt("id"));
employee.setFirstName(rs.getString("first_name"));
employee.setLastName(rs.getString("last_name"));
employee.setEmail(rs.getString("email"));
employee.setHireDate(rs.getDate("hire_date"));
employees.add(employee);
}
} catch (Exception e) {
e.printStackTrace();
}
request.setAttribute("employees", employees);
RequestDispatcher dispatcher = request.getRequestDispatcher("/employees.jsp");
dispatcher.forward(request, response);
}
}
<%@ page import="java.util.List" %>
<%@ page import="employee.Employee" %>
<%@ page import="java.util.ArrayList" %>
<%
List<Employee> employees = (List<Employee>) request.getAttribute("employees");
if (employees == null || employees.isEmpty()) {
System.out.println("Employee list is empty or null.");
}
%>
<table border="1">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Hire Date</th>
</tr>
<%
if (employees != null) {
for (Employee employee : employees) {
%>
<tr>
<td><%= employee.getId() %></td>
<td><%= employee.getFirstName() %></td>
<td><%= employee.getLastName() %></td>
<td><%= employee.getEmail() %></td>
<td><%= employee.getHireDate() %></td>
</tr>
<%
}
} else {
%>
<tr>
<td colspan="5">No data found.</td>
</tr>
<%
}
%>
</table>
数据明显存在,并且似乎已正确连接,但当我运行它时,它是空的。
您没有在 servlet 中设置 request 属性,因为您使用了请求 url
http://localhost:8080/ex01_war/employees.jsp
代码
request.getAttribute("employees")
返回 null
。
当 JSP 从 servlet 转发到 JSP 时,它会保留其属性。
但是你应该使用
http://localhost:8080/ex01_war/employees
。
如果您在浏览器中手动输入此 url,则会执行 Http GET 请求,该请求由
doGet()
方法处理。然后填写员工列表并设置请求属性request.setAttribute("employees", employees);
,然后转发到 JSP。
当 JSP 渲染时,它将获取先前在 servlet 中设置的 request 属性。因此,它不会是
null
并且列表将在 for
循环中迭代。