【问题标题】:Retrieving image from database in hibernate在休眠中从数据库中检索图像
【发布时间】:2017-05-21 11:42:58
【问题描述】:

这是我的 servlet,名为 showimage.java

 protected void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {

        session=sessionFactory.openSession();
        session.beginTransaction();         

        try{            
        Tab person = (Tab) session.get(Tab.class,new Integer(1));     
        byte[] photoBytes = person.getEImage();        
        try (ServletOutputStream sos = res.getOutputStream()) {
             sos.write(photoBytes);
            }
        }
        catch(Exception e){
        e.printStackTrace();        
        session.getTransaction().commit();
        session.close();
        a++;
        }        
}

下面是我的 jsp 页面,名为 showimage.jsp。由于这个 Tab person = (Tab) session.get(Tab.类,新整数(1));。有人可以建议替换 new Integer(1) 以便每次调用 servlet 时,我都能获得新的 Id 动态从数据库中获取。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP List Users Records</title>
</head>
<body>
    <sql:setDataSource
        var="myDS"
        driver="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/project"
        user="root" password="lovemapa!23"
    />

    <sql:query var="listUsers"   dataSource="${myDS}">
        SELECT * FROM tab;
    </sql:query>

    <div align="left">
        <table border="1" cellpadding="5">
            <caption><h2>List of users</h2></caption>
            <tr>
                <th>ID</th>
                 <th>Image</th>
            </tr>
            <c:forEach var="user" items="${listUsers.rows}">
                <tr>
                    <td><c:out value="${user.E_id}" /></td>
                   <td><img src="showimage?id=${user.E_id}"></td>
                </tr>
            </c:forEach>
        </table>
    </div>
</body>
</html>

【问题讨论】:

    标签: hibernate jsp servlets


    【解决方案1】:

    您将 id 作为 URL 参数传递:showimage?id=${user.E_id}。 在您的 servlet 中,您可以将该值检索为

    Integer id = new Integer(req.getParameter("id"));
    Tab person = (Tab) session.get(Tab.class, id);
    

    【讨论】:

    • 还有一个疑问 先生,showimage 中的这个“id”?id=${user.E_id} ...这是预先写好的还是我们必须定义它?以及如何在 URL 参数中传递 id,先生
    • 非常感谢先生,这真的很有用。非常感谢:)上帝保佑你:)
    猜你喜欢
    • 2021-10-14
    • 1970-01-01
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多