Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display multiple Images from a DB on a JSP Page.

Status
Not open for further replies.

abc73

Programmer
Apr 28, 2004
89
US
Hi all, please give me your solution, thanks.
I have many images, stored in Oracle database (BLOB),

How to display all of them (images) on a JSP page?

I have a working solution that only shows one image based on the specific image-name passed. But if no image found in the Database with that image-name then I want to show all the images at once on a JSP page. Can someone help please.

Thanks.
 
I would have a servlet that retrieves a single image, and then in your JSP create a bunch of <img src="myservlet"/> tags, cerate as you need them (ie dynamically)

--------------------------------------------------
Free Database Connection Pooling Software
 
any small sample will help as I am new to JSP/Servlets. I am using a servlet that queries through a database and gets the images. JSP page calls in the servlet.

Thanks
 
Show us your code for currently displaying the images ...

--------------------------------------------------
Free Database Connection Pooling Software
 
Code:
[b]Here is the JSP page that calls the servlet to show the images:[/b]

<%@ page contentType="text/html;charset=windows-1252"%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>untitled</title>
  </head>
  <body valign=center>
      <table>
        <tr>
            <td>
                <img src="/test-testimgs-context-root/servlet/test1.images.servlet.GetImage">
            </td>
        <tr>
      </table>
  </body>
</html>


[b]Here is the code that resides in the service method of GetImage servlet:[/b]

public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{        
	OutputStream stream = response.getOutputStream();  
	Blob imgBlob = null;
	ResultSet rs = null;
	byte[] imbBytesAry = null;
	
	try
	{
		ImageTest imgtest = new ImageTest();
		rs = imgtest.get("img1.tif");

		if(rs != null)
		{
			while(rs.next())
			{
				imgBlob = rs.getBlob(1);
				int len = new Integer( new Long( imgBlob.length()).toString() ).intValue();
				imbBytesAry = imgBlob.getBytes(1,len);
				if (imbBytesAry.length == 0 || imbBytesAry == null)
				{
					noImageFound(request, response);  
				}
				else
				{
					stream.write(imbBytesAry);
				}
			}
		}
		else
		{
			noImageFound(request, response);
		}
	}
	catch (Exception e)
	{
		noImageFound(request, response);
	}  
}



[b]Here is the code that resides in the get method of ImageTest class:[/b]

public ResultSet get(String fileName) throws DatabaseConnectionException, SQLException, Exception
{
Connection connection=null;
ResultSet rs = null;
ResultSet rs1 = null;
Blob imgBlob = null;
try
{
	connection = new ConnectionProvider().getCConnection() ;
	connection.setAutoCommit(false);
	Statement stmt = connection.createStatement();
	
	String sql = "SELECT IMG FROM IMAGE_TABLE WHERE IMAGE_NAME = ? ";  
	PreparedStatement pstmt = connection.prepareStatement(sql);
	pstmt.setString(1,fileName);
	rs = pstmt.executeQuery();
	
	boolean flag = rs.next();
	if(flag)
	{
		rs.beforeFirst();
	}
	else
	{
		sql = "SELECT IMG FROM IMAGE_TABLE";  
		rs1 = stmt.executeQuery(sql);
		boolean flag_all = rs1.next();
		if(flag_all)
		{
			rs1.beforeFirst();
		}
		else
			rs1 = null;
	}
	return rs;
}

As you see in the JSP page I am calling GetImage servlet, whose service method does all the processing i.e. calls ImageTest get
method that queries the database and returns the result. Eventually what I would like is to pass the filename along with other parameters
from JSP page and the servlet returns the images based on that. Currently I have just hard coded a value for testing puposes. Any help is appreciated.

Thanks
 
Can anyone please help me out here!

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top