【发布时间】: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>
【问题讨论】: