views:

123

answers:

4

Have a look at the following code snippet.

response.setContentType("image/gif");
String url="jdbc:oracle:thin:@localhost:1521:xe";
String username="xyz";
String password="abc";

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=DriverManager.getConnection(url,username,password);
String sql="Select name,description,image from pictures";
PreparedStatement stmt=conn.prepareStatement(sql);
ResultSet resultSet=stmt.executeQuery();
ServletOutputStream sos=response.getOutputStream();

while(resultSet.next()) {
    byte[] buffer=new byte[1];

    InputStream is=resultSet.getBinaryStream(3);
    while(is.read(buffer)>0){
      sos.write(buffer);
}
sos.println();
sos.flush();
}

sos.close();
conn.close();

I am trying this code to display images which are retieved from the database.This code is supposed to retrive multiple images from multiple rows which are stored in the database.But this code displays a single image.Kindly help me.

+3  A: 

Why would it display multiple images ? You're setting the content type of the response to image/gif, and that means the browser will expect a single image. However you're streaming multiple images into the response stream.

So I suspect the browser is taking just the first image. It could just as well reject the whole response as being corrupted (since we have multiple images streamed together).

You need to identify which image you retrieve for each request, and modify the above to extract a single image (by amending your SQL appropriately).

Brian Agnew
So how should I set the response type then so that it displays multiple images in a single page??It would be much better if I am able to display the name of that image as text.Is it possible??
DEEPMALA
You need to (say) build an HTML page containing a list of references to your images (number 1,2,3) etc. So you'll need to have a JSP that requests the list of images and their name, and embeds <img> links to those images. It's a 2-stage process.
Brian Agnew
+1  A: 

You can't return multiple images at once. You can think of the access to the servlet as an access to a file.

Here you created a "file" which contains multiple images. Most of the image-renderers will only see the first image stored in the "file".

If you want to have more than one image, you have to do more than one request (or open more than one file).

Colin Hebert
+1  A: 

As already pointed out by Brian and Colin you can't have multiple images , all being accessed simultaneously. It seems your task is to have many images in the same page. then one of the way as suggested by Brian (using JSP that calls for the images). The other way is to divide your page into multiple frame sets which themselves can call the images in the same way JSP does.

CadetNumber1
+3  A: 

In HTML, an image is to be represented by the <img> element. It has a src attribute which should point to an URL of the image. The URL should point to a servlet which returns a single image by the given path in URL http://example.com/imageservlet/filename.gif or by a request parameter in URL http://example.com/imageservlet?id=123.

Your functional requirement is apparently to show them all in a single JSP page. To achieve this, you need to print multiple <img> elements, each pointing to a different unique image. Assuming that it's the image's filename which is unique in the database, you need to collect them in a List<String> first. You can do this in doGet() method of another servlet which in turn forwards the request to JSP for display:

List<String> imageNames = imageDAO.listNames();
request.setAttribute("imageNames", imageNames);
request.getRequestDispatcher("/WEB-INF/images.jsp").forward(request, response);

The listNames() method should basically return the result of SELECT name FROM pictures in flavor of a List<String>. Then in the JSP you can use JSTL c:forEach tag to iterate over imageNames and print a separate <img> element for each with the name in the URL.

<c:forEach items="${imageNames}" var="imageName">
    <img src="imageservlet/${imagename}">
</c:forEach>

Finally, in the doGet() of the imageservlet which is mapped on an url-pattern of /imageservlet/*, obtain the imagename by HttpServletRequest#getPathInfo().

String imagename = request.getPathInfo().substring(1);
InputStream input = imageDAO.find(imagename);
OutputStream output = response.getOutputStream();
// Write input to output the usual Java IO way.

The find() method should basically return the result of SELECT image FROM pictures WHERE name = ?.

To get it to run, just open the URL of the first servlet. E.g. http://example.com/images when it's mapped on an url-pattern of /images. It will obtain the names, forward to JSP, let JSP display multiple <img> elements, which each in turn calls the imageservlet to obtain the image content.

BalusC
actually after the link to servlet is provided in src attribute, do i have to create different servlets for as many rows are there in the database, but then ultimately i have to know no of rows to create as many servlets before hand.Please suggest.
DEEPMALA
You don't need to create different servlets. Just make the difference in pathinfo or request parameter of URL.
BalusC