File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Servlets
Reply locked New topic

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.setContentType("image/jpeg"); //JPG file
conn = DisableText.getConnection(); // Connecting to the database
pstmt = conn.prepareStatement("select image from image_upload where image_name=?");
rs = pstmt.executeQuery();
byte[] imgBytes = rs.getBytes(1);
catch (Exception e)
throw new ServletException("Unable to print image"+e.getMessage());

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

Can any one help me please.

I agree. Here's the link:
subject: Display of image in JSP from data base using servlet
It's not a secret anymore!