我使用servlet从数据库中获取数据并在JSP中显示。我得到了字符串,但没有得到图像。
我得到这样的输出:English [B@12fd16f
这是我的servlet类:
@WebServlet("/ReadFindServlet")
public class ReadFindServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private List<Find> findList = new ArrayList<>();
public List<Find> getFindList() {
return findList;
}
public void setFindList(List<Find> findList) {
this.findList = findList;
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
ResultSet resultSet = null;
try {
resultSet = LoginDao.readFind();
while(resultSet.next()){
Find find = new Find();
find.setSubject(resultSet.getString(1));
find.setImage(resultSet.getBytes(2));
response.setContentType("image/jpeg");
findList.add(find);
}
setFindList(findList);
request.setAttribute("findList", findList);
RequestDispatcher requestDispatcher = request.getRequestDispatcher("jsp/find.jsp");
requestDispatcher.forward(request, response);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Find.java
public class Find {
private String subject;
private byte[] image;
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
}
jsp -
<c:forEach items="${findList}" var="findRecord">
<h1>Here are details - </h1>
${findRecord.subject}
<img src="/ReadFindServlet"${findRecord.image }/>
</c:forEach>
我也试过了 -
<img src="/ReadFindServlet/${findRecord.image }"/>
<img src="{findRecord.image }"/>
但没效果。
请帮忙。