我正在使用eclipse创建一个动态Web项目,遵循MVC设计。我目前正在尝试从我的sql表中获取数据以显示在JSP页面上,但它只是返回为空白。我使用JNDI datasorce连接工作正常。当我将项目部署到服务器时,表头显示正常,但下面没有数据。 这是我的代码
JSP代码
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>View All Students</title>
</head>
<body>
<h1>Students List</h1>
<table cellpadding="5" border=1>
<tr valign="bottom">
<th><%= request.getAttribute("Student_Id") %></th>
<th>First Name</th>
<th>Last Name</th>
<th>Week 1</th>
<th>Week 2</th>
<th>Week 3</th>
<th>Week 4</th>
<th>Week 5</th>
<th>Week 6</th>
<th>Week 7</th>
<th>Week 8</th>
<th>Week 9</th>
<th>Week 10</th>
<th>Week 11</th>
<th>Week 12</th>
</tr>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:forEach var="student" items="${viewAllStudents}">
<tr valign="top">
<td><p>${student.Student_Id}</td>
<td><p>${student.firstName}</td>
<td><p>${student.lastName}</td>
<td><p>${student.Week1}</td>
<td><p>${student.Week2}</td>
<td><p>${student.Week3}</td>
<td><p>${student.Week4}</td>
<td><p>${student.Week5}</td>
<td><p>${student.Week6}</td>
<td><p>${student.Week7}</td>
<td><p>${student.Week8}</td>
<td><p>${student.Week9}</td>
<td><p>${student.Week10}</td>
<td><p>${student.Week11}</td>
<td><p>${student.Week12}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
Servelt(控制器)代码
package uk.ac.qub.PTStudentView;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import uk.ac.qub.Beans.Student;
import uk.ac.qub.LoginController.Account;
/**
* Servlet implementation class PTStudentViewController
*/
@WebServlet("/PTStudentViewController")
public class PTStudentViewController extends HttpServlet {
private static final long serialVersionUID = 1L;
private DataSource ds;
/**
* @see HttpServlet#HttpServlet()
*/
public PTStudentViewController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
try {
InitialContext initContext = new InitialContext();
Context env = (Context) initContext.lookup("java:comp/env");
ds = (DataSource) env.lookup("jdbc/testdb");
} catch (NamingException e) {
throw new ServletException();
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
List<Student> viewAllStudents = PTViewStudent.list();
request.setAttribute("students", viewAllStudents); // Will be available as ${students} in JSP
request.getRequestDispatcher("ViewAllStudentsPage.jsp").forward(request, response);
} catch (SQLException e) {
throw new ServletException("Cannot obtain products from DB", e);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
Sql statment的类
package uk.ac.qub.PTStudentView;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import uk.ac.qub.Beans.Student;
public class PTViewStudent {
private static Connection conn;
public PTViewStudent (Connection conn){
this.conn = conn;
}
public static List<Student> list() throws SQLException{
String sql = "select * from student where Student_Id = 666";
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
List<Student> viewAllStudents = new ArrayList<>();
while(rs.next()){
Student student = new Student();
student.setId(rs.getInt("Student_Id"));
student.setFirstName(rs.getString("firstName"));
student.setLastName(rs.getString("lastName"));
student.setWeek1(rs.getDouble("Week1"));
student.setWeek2(rs.getDouble("Week2"));
student.setWeek3(rs.getDouble("Week3"));
student.setWeek4(rs.getDouble("Week4"));
student.setWeek5(rs.getDouble("Week5"));
student.setWeek6(rs.getDouble("Week6"));
student.setWeek7(rs.getDouble("Week7"));
student.setWeek8(rs.getDouble("Week8"));
student.setWeek9(rs.getDouble("Week9"));
student.setWeek10(rs.getDouble("Week10"));
student.setWeek11(rs.getDouble("Week11"));
student.setWeek12(rs.getDouble("Week12"));
viewAllStudents.add(student);
}
return viewAllStudents;
}
}
谢谢你的帮助
答案 0 :(得分:0)
我发现了你的错误!
是的,所以在你的jsp中,你有这行代码
<c:forEach var="student" items="${viewAllStudents}">
这意味着,在请求范围中查找名为viewAllStudents的列表,然后循环遍历所有
但是,在您的实际servlet代码中,这是有效的,你有这个:
List<Student> viewAllStudents = PTViewStudent.list();
request.setAttribute("students", viewAllStudents); // Will be available as ${students} in JSP
request.getRequestDispatcher("ViewAllStudentsPage.jsp").forward(request, response);
这也是有效的,但是,您需要将JSTL更改为此
<c:forEach var="student" items="${students}">