Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • 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
  • Quote
  • 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
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please delete your other thread
http://www.coderanch.com/t/362687/Servlets/java/Display-image-JSP-data-base
 
Mandar Max
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by a broken image? Is it distorted or it looks totally different from what you expected? Try specifying a size (width & height) in the <IMG> tag.
 
Shrikant Kulkarni
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Broken image means there is a space in the browser for Image but image isn ot displyed. Like we use to get this when we mention a image but that specific image the browser can't find.

I mentioned height and width also but not helped.
Any idea please.

Shrikant
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. How big is the image ?
2. What is "op" ?
 
Mandar Max
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably you should make sure that the file you are reading is indeed a jpeg file. One of the simple ways to test this is to write the bytes to a file and try reading it using the browser / image viewer program. Normally, even if the image file extension is incorrect or invalid, 'Windows image and fax viewer' should be able to render the file if it's a valid image file, whereas if the extension does not match the actual file type, Internet explorer will not render the image, but just display a cross.
 
Shrikant Kulkarni
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Size of the image is aroung 150KB
op : It is an outputstream which i useed earlier but now i am not using it.. so op.flush(), op.close() doesn't have any significance.


All the images are jpeg only.
 
Ravindra Rawat
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<IMG src="<%=request.getContextPath() %>/DisableText">

Just to confirm? where is the name parameter required for servlet?

- Ravindra
 
Shrikant Kulkarni
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to confirm? where is the name parameter required for servlet?

Hi

Name paramter required for servlet means??
<servlet> and <servlet-mapping> entries right??

If yes, then i have made entries in web.xml and it is as below

<servlet>
<servlet-name>download</servlet-name>
<servlet-class>disable.DisableText</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>download</servlet-name>
<url-pattern>/download</url-pattern>
</servlet-mapping>
 
Howard Ralston
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think he meant that you don't pass the name of the image in the code you gave.



Might instead be:



Also, if your url-pattern is /download, shouldn't you use that instead of /DisableText?
[ May 24, 2006: Message edited by: Howard Ralston ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic