Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
    Bookmark Topic Watch Topic
  • New Topic

Display of image in JSP from data base using servlet

 
Shrikant Kulkarni
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi All,

I am getting the image from the database (in a servlet), I want to display that image in JSP. But in JSP i am getting a broken image instead of an proper image.

Here is the servlet code :

public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
{
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String strImageName = req.getParameter("name");
res.reset();
res.setContentType("image/jpeg"); //JPG file
try{
conn = DisableText.getConnection(); // Connecting to the database
pstmt = conn.prepareStatement("select image from image_upload where image_name=?");
pstmt.setString(1,strImageName);
rs = pstmt.executeQuery();
while(rs.next())
{
byte[] imgBytes = rs.getBytes(1);
res.setContentLength(imgBytes.length);
res.getOutputStream().write(imgBytes);
}
op.flush();
op.close();
}
}
catch (Exception e)
{
throw new ServletException("Unable to print image"+e.getMessage());
}
}


Here is the JSP code:
<html>
<body>
<form name="delete" action="download" method="post" >
<table>
<tr>
<td>
<IMG src="<%=request.getContextPath() %>/DisableText"> // DisableText is the servlet class name
</td>
</tr>
</table>
</form>
</body>
</html>


Can any one help me please.

Shrikant
 
    Bookmark Topic Watch Topic
  • New Topic