This is my servlet named 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++;
}
}
And below is my jsp page named showimage.jsp.I'm getting same images from database in image colomn due to this Tab person = (Tab) session.get(Tab.class,new Integer(1));. Can someone suggest to replace new Integer(1) so that every time the servlet is called , i am able to get new Id from database dynamically.
<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>