Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Need to display multiple images from database on a webpage.

    hi,

    I am able to display a single image from the database on a webpage using the

    following code:

    rs1 = st1.executeQuery("select image from pictures where id='5'");
    if(rs1.next()){
    int len = imgLen.length();
    byte [] rb = new byte[len];
    InputStream readImg = rs1.getBinaryStream(1);
    int index=readImg.read(rb, 0, len);
    System.out.println("index"+index);
    st1.close();
    response.reset();
    response.setContentType("image/jpg");
    response.getOutputStream().write(rb,0,len);
    response.getOutputStream().flush();


    Can any one help me to get the code to display more than one image on a *.jsp

    file. I tried putting the while loop, but it is'nt doing the job.

    Thanks.

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    you can set the content type only once so, you should have an image serving jsp which you can use in your other jsp...for eg.

    Code:
    <img src="image.jsp?id=1" />
    where image.jsp reads the request parameter and serves the respective image
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up issue solved

    Thanks shyam,

    I got the solution. We need to call the servlet displaying image in the "src" attribute of

    <img> html tag.

    eg: <img src="/ImageServlet?imageno=1">

  • #4
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I want sample code for displaying img from database on web pg..wil u plz help me?

  • #5
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <img src="/ImageServlet?imageno=1">

    I dnt kw , how this works???

  • #6
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I used struts 2 and hibernate to display multiple images on jsp

    Query:

    String mQuery=select imageid from ImageRepository
    return the list of imageids

    Struts 2:

    method in struts 2 action class

    public String generateImages()
    {

    List imageids=//get list of image ids from db

    Long imgData1= (Long) imageids.get(0);
    Long imgData2= (Long) imageids.get(1);

    //Set list of image ids on request attribute
    request.setAttribute("imgData1", imgData1);
    request.setAttribute("imgData2", imgData2);

    }

    Sample JSP page

    <%
    Long imgData1=(Long)request.getAttribute("imgData1");
    Long imgData2=(Long)request.getAttribute("imgData2");
    long a;
    long b;
    String s=null;
    String s1=null;

    if(null!=imgData1)
    {
    a=imgData1.longValue();
    s="ImageTextMappingQueue_generateImageFront.action?imgData="+a;
    }

    if(null!=imgData2)
    {
    b=imgData2.longValue();
    s1="ImageTextMappingQueue_generateImageFront.action?imgData="+b;
    }
    request.setAttribute("imgData1",imgData1);
    %>


    <%if(null!=s)
    {%>

    <img src="<%=s%>">
    <%}
    else
    {
    %>
    Image not found
    <%
    }%>

    <%if(null!=s1)
    {%>

    <img src="<%=s1%>">
    <%}
    else
    {
    %>
    <br>
    Image not found
    <%
    }%>


    Sample servlet or struts 2 action class

    public String generateImageFront()
    {

    Long l=new Long(request.getParameter("imgData"));
    ImageRepository mImageRepository=//query to return blob image using image id
    Blob mBlob=mImageRepository.getImageBlob();

    byte[] imgData1 = null ;

    imgData1=mBlob.getBytes(1,(int)mBlob.length());
    response.getOutputStream().write(imgData1);

    return Action.NONE;
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •