File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Display of image in JSP from data base using servlet    Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Reply locked New topic
Author

Display of image in JSP from data base using servlet

Shrikant Kulkarni
Ranch Hand

Joined: May 10, 2005
Posts: 42
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
 
jQuery in Action, 2nd edition
 
subject: Display of image in JSP from data base using servlet
 
Similar Threads
search
weird functioning of servlet
javax. servlet. Servlet Exception: java. sql. SQLException
Display of image in JSP from data base using servlet
Retreiving a image from a database using servlet