aspose 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
JavaRanch » Java Forums » Java » Servlets
Bookmark "Display of image in JSP from data base using servlet    " Watch "Display of image in JSP from data base using servlet    " 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
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Please delete your other thread
http://www.coderanch.com/t/362687/Servlets/java/Display-image-JSP-data-base


[My Blog]
All roads lead to JavaRanch
Mandar Max
Ranch Hand

Joined: Mar 14, 2006
Posts: 38
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.


"The trouble with doing something right the first time is that nobody appreciates how difficult it was!"
Shrikant Kulkarni
Ranch Hand

Joined: May 10, 2005
Posts: 42
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

1. How big is the image ?
2. What is "op" ?
Mandar Max
Ranch Hand

Joined: Mar 14, 2006
Posts: 38
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

Joined: May 10, 2005
Posts: 42
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

Joined: Dec 09, 2004
Posts: 34
<IMG src="<%=request.getContextPath() %>/DisableText">

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

- Ravindra
Shrikant Kulkarni
Ranch Hand

Joined: May 10, 2005
Posts: 42
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

Joined: Jun 25, 2001
Posts: 105
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 ]

<a href="http://www.getlocaldeals.com" target="_blank" rel="nofollow">Free local coupons</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Display of image in JSP from data base using servlet