【发布时间】:2013-11-29 14:47:30
【问题描述】:
背景:
我之前编写了一个 servlet,它创建了动态网页,它可以在其中读取和写入 MySQL 数据库。当我从中读取时,我将结果存储在 ResultSet 中,然后显示此信息。一切正常,我被告知我应该使用 JSP 和 JSTL 重新编写它。为此,在我的 servlet 中,我创建了一个 ResultSet 并将每一列的结果存储在一个单独的 ArrayList 中。在我的 JSP 中,我试图遍历 ArrayLists,但是网页是空白的。由于我使用的是 NetBeans,它通常会指示任何错误,但似乎没有,所以我猜测要么是逻辑错误,要么是运行时错误(这并不能完全缩小范围)。我试过简单地打印 ArrayList 的大小,但即使这样也没有显示!我的猜测是永远不会填充 ArrayList 或者它没有正确发送到 JSP。
请注意,我的数据库已填充,我可以对其进行写入。我浏览了各种网站,看看是否有任何帮助,但到目前为止还没有。我还阅读了我的讲义,我的语法似乎是正确的。
作为作业的一部分,我们需要在 processRequest(request, response) 中编写所有 servlet 代码。 doPost 和 doGet 都调用此方法。
我的 Servlet:
protected void processRequest(HttpServletRequest request, HttpServletResponse response) {
response.setContentType("text/html;charset=UTF-8");
ResultSet res = null;
ArrayList<String> bookIDs, bookTitles, bookAuthors;
DBUtil util = new DBUtil();
try {
res = BookDB.showBooks();
bookIDs = new ArrayList<String>();
bookTitles = new ArrayList<String>();
bookAuthors = new ArrayList<String>();
while(res.next()) {
bookIDs.add(res.getString("bookID"));
bookTitles.add(res.getString("title"));
bookAuthors.add(res.getString("author"));
}
request.setAttribute("IDHolder", bookIDs);
request.setAttribute("titlesHolder", bookTitles);
request.setAttribute("authorHolder", bookAuthors);
RequestDispatcher booksDispatcher = request.getRequestDispatcher("jspShowAll.jsp");
booksDispatcher.forward(request, response);
} catch (SQLException sqle) {
Logger.getLogger(ShowAllBooks.class.getName()).log(Level.SEVERE, null, sqle);
} catch (IOException iox) {
Logger.getLogger(ShowAllBooks.class.getName()).log(Level.SEVERE, null, iox);
} catch (ServletException ex) {
Logger.getLogger(ShowAllBooks.class.getName()).log(Level.SEVERE, null, ex);
}
}
创建结果集(在单独的包中):
public static ResultSet showBooks() throws SQLException {
ConnControl myConnPool=new ConnControl();
Statement s=null;
ResultSet res=null;
Connection myConn=myConnPool.connect();
String queryStm= "SELECT * FROM books;";
s=myConn.createStatement();
res = s.executeQuery(queryStm);
return res;
}
此方法是从我以前的网络应用程序中复制和粘贴的,效果很好。表和数据库是一样的。
我的 JSP:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
</head>
<body>
<c:forEach var="ids" items="${IDHolder}">
<p>${ids}</p>
</c:forEach>
<c:forEach var="titles" items="${titlesHolder}">
<p>${titles}</p>
</c:forEach>
<c:forEach var="authors" items="${authorHolder}">
<p>${authors}</p>
</c:forEach>
</body>
</html>
我的 Java Bean:
public class Book {
private int id;
private String title;
private String author;
public Book() {
}
public Book(int id, String title, String author) {
this.id = id;
this.title = title;
this.author = author;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public void setId(int id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
public void setAuthor(String author) {
this.author = author;
}
}
【问题讨论】:
-
你有什么异常吗?
-
@Masud 这就是问题所在,这是一个完全空白的网页。我没有得到任何类型的例外。当我右键单击查看源代码时,它也是空白的(只是希望那里有东西)。
-
检查您的日志。您可能有一个异常,但由于您捕获它并记录它,因此页面中没有显示任何内容。
-
@JBNizet 我从异常中删除了 Logger.getLogger()... 并将其替换为 printStackTrace()。我发现我忘了包含 JSTL 库。