在JSP中显示数据库结果

发布于 2024-10-02 13:46:23 字数 1314 浏览 0 评论 0原文

我有一个控制器 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

删除→记忆 2024-10-09 13:46:23

使用 JSTL c:forEach 标记。如果您的servlet容器不支持它(例如Tomcat),那么您需要删除 jstl-1.2.jar/WEB-INF/lib 中。然后声明JSTL核心标签库 根据其文档位于 JSP 页面顶部。

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

然后您可以在 JSP 中使用任何 JSTL 核心标记。您在具有属性名称 customerList 的请求范围。因此可以通过 EL 中的 ${customerList} 获取它。将其提供给 items 属性并相应地渲染

<table>
    <c:forEach items="${customerList}" var="customer">
        <tr>
            <td><c:out value="${customer.id}" /></td>
            <td><c:out value="${customer.name}" /></td>
            <td><c:out value="${customer.address}" /></td>
        </tr>
    </c:forEach>
</table>
            

顺便说一下, 不是必需的,但如果它涉及用户控制的输入,则很有用,因为它可以防止 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.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

Then you can use any of the JSTL core tags in the JSP. You've put a Vector<Customer> (eek, a legacy class .. rather use List<Customer> customers = new ArrayList<Customer>() instead) in the request scope with the attribute name customerList. So it's available by ${customerList} in EL. Feed it to the items attribute of <c:forEach> and render a <table> accordingly.

<table>
    <c:forEach items="${customerList}" var="customer">
        <tr>
            <td><c:out value="${customer.id}" /></td>
            <td><c:out value="${customer.name}" /></td>
            <td><c:out value="${customer.address}" /></td>
        </tr>
    </c:forEach>
</table>
            

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文