在JSP中显示数据库结果
我有一个控制器 servlet,它将请求转发到模型 servlet。现在,当模型从数据库获取结果时,我将其转发到 jsp。我不确定在 jsp 中写什么,因为它需要显示一个表customerList.here 是我的模型 servlet 的一部分:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
Connection connection = getDatabaseConnection();
request.setAttribute("customerList", getCustomerList(connection));
closeDatabaseConnection(connection);
}
private Vector<Customer> getCustomerList(Connection con)
{
String sqlStr =
"SELECT * " +
"FROM Customer " +
"ORDER BY Name";
PreparedStatement stmt = null;
ResultSet rs = null;
Vector<Customer> customers = new Vector<Customer>();
try
{
stmt = con.prepareStatement(sqlStr);
rs = stmt.executeQuery();
while (rs.next())
{
Customer customer = new Customer();
customer.setId(rs.getInt("Id"));
customer.setName(rs.getString("Name"));
customer.setAddress(rs.getString("Address"));
customers.add(customer);
}
rs.close();
stmt.close();
}
catch (SQLException sqle)
{
sqle.printStackTrace();
}
finally
{
return customers;
}
I have a controller servlet which forward the request to the model servlet.Now when the model gets the results from the database, I am forwarding it to the jsp.I am not sure what to write in the jsp because it need to show a table of customerList.here is part of my model servlet:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
Connection connection = getDatabaseConnection();
request.setAttribute("customerList", getCustomerList(connection));
closeDatabaseConnection(connection);
}
private Vector<Customer> getCustomerList(Connection con)
{
String sqlStr =
"SELECT * " +
"FROM Customer " +
"ORDER BY Name";
PreparedStatement stmt = null;
ResultSet rs = null;
Vector<Customer> customers = new Vector<Customer>();
try
{
stmt = con.prepareStatement(sqlStr);
rs = stmt.executeQuery();
while (rs.next())
{
Customer customer = new Customer();
customer.setId(rs.getInt("Id"));
customer.setName(rs.getString("Name"));
customer.setAddress(rs.getString("Address"));
customers.add(customer);
}
rs.close();
stmt.close();
}
catch (SQLException sqle)
{
sqle.printStackTrace();
}
finally
{
return customers;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 JSTL
c:forEach
标记。如果您的servlet容器不支持它(例如Tomcat),那么您需要删除 jstl-1.2.jar 在/WEB-INF/lib
中。然后声明JSTL核心标签库 根据其文档位于 JSP 页面顶部。然后您可以在 JSP 中使用任何 JSTL 核心标记。您在具有属性名称
customerList
的请求范围。因此可以通过 EL 中的${customerList}
获取它。将其提供给
的items
属性并相应地渲染。
顺便说一下,
不是必需的,但如果它涉及用户控制的输入,则很有用,因为它可以防止 XSS 攻击。也就是说,您的 JDBC 部分可以做得更好。发生异常时,它仍然对资源泄漏敏感。
另请参阅:
Use JSTL
c:forEach
tag. If your servletcontainer doesn't support it (e.g. Tomcat), then you need to drop jstl-1.2.jar in/WEB-INF/lib
. Then declare the JSTL core taglib in top of JSP page as per its documentation.Then you can use any of the JSTL core tags in the JSP. You've put a
Vector<Customer>
(eek, a legacy class .. rather useList<Customer> customers = new ArrayList<Customer>()
instead) in the request scope with the attribute namecustomerList
. So it's available by${customerList}
in EL. Feed it to theitems
attribute of<c:forEach>
and render a<table>
accordingly.The
<c:out>
is by the way not necessary, but useful if it concerns user-controlled input since it prevents XSS attacks.That said, your JDBC part can be done better. It's still sensitive to resource leaking in case of exceptions.
See also: