This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JSP and the fly likes Displaying images on JSP page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Displaying images on JSP page" Watch "Displaying images on JSP page" New topic
Author

Displaying images on JSP page

chetan dhumane
Ranch Hand

Joined: Jan 07, 2009
Posts: 629

I want to display images on my JSP page .I have images stored on my MySQL database , I am copying those images to c:\ ,the images get displayed correctly on localhost but when I run the same application on other machines images are not getting displayed.

How to store those images as cookies in clients web browser to get it displayed in clients machine also.

Thanks in advance.
Chetan D.


http://www.androcid.com/
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Search this same section with "JSP display image", this is discussed lots of time.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

chetan dhumane wrote:the images get displayed correctly on localhost but when I run the same application on other machines images are not getting displayed.

Paste the way you are accessing the images in JSP, and make sure you are not copying "localhost" in URL of images.

chetan dhumane wrote:
How to store those images as cookies in clients web browser to get it displayed in clients machine also.

You really don't need cookies to store images on browser, lets browser cache it for you
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

chetan dhumane wrote:.

How to store those images as cookies in clients web browser to get it displayed in clients machine also.

Chetan D.


Thats Strange. Cookies are meant to store client state and information and not for these things.


I am copying those images to c:\ ,the images get displayed correctly on localhost but when I run the same application on other machines images are not getting displayed.


The images has to be inside the webserver directories to be accessible from other machines
Vinoth Thirunavukarasu
Ranch Hand

Joined: Dec 18, 2008
Posts: 164

The bellow link will help you
webpage


Java Best Practices
Linux Best Practices
Amortization Calculator

chetan dhumane
Ranch Hand

Joined: Jan 07, 2009
Posts: 629

I want to embed that image into img tag and then display it.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Balu Sadhasivam wrote:
The images has to be inside the webserver directories to be accessible from other machines
chetan dhumane
Ranch Hand

Joined: Jan 07, 2009
Posts: 629

Can anyone help me to tell getting images from database and displaying it on JSP page using img tag .
I have also written beans for the same to store the images in the database and to retrieve the recordset using beans.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Looks like our efforts goes in vain

OK, still some pointers-

  • Google it, Display images stored into database in a JSP
  • Search the same forum
  • Tell us the details about where you get stuck, form your last reply, I conclude that, you're facing problem in <img src="\path" .../>, for that, Balu suggested about saving image in to web server (If its Tomcat, then store image into "webapps/YOUR_APP/images_from_db/myimage.jpg", for which resulted img tag)

  • chetan dhumane
    Ranch Hand

    Joined: Jan 07, 2009
    Posts: 629

    I got stuck about getting the images from db and storing it into images folder.
    I am able to store images into MySQL database using BLOB format.But how to get that image and display into the <img> tag of html.
    I want my JSP page to store those images automatically to images folder of my webapp at runtime.

    I dont want to set contentType to images/jpg ,It causes to "display an message box about downloading the particular image"
    I think you got my question.

    I also want to display multiple images on single page.

    Sagar Rohankar
    Ranch Hand

    Joined: Feb 19, 2008
    Posts: 2902
        
        1

    chetan dhumane wrote:I got stuck about getting the images from db and storing it into images folder.



    This is general idea, please check out the API for correct methods.

    JSP
    chetan dhumane
    Ranch Hand

    Joined: Jan 07, 2009
    Posts: 629

    But when I access my project URL from another machine ,images do not get displayed.
    I can see images on my localhost only.
    Sagar Rohankar
    Ranch Hand

    Joined: Feb 19, 2008
    Posts: 2902
        
        1

    Just paste the JSP code you're using to display images.
    chetan dhumane
    Ranch Hand

    Joined: Jan 07, 2009
    Posts: 629

    Sagar Rohankar
    Ranch Hand

    Joined: Feb 19, 2008
    Posts: 2902
        
        1


    You couldn't get access to this c: drive when you access the image from another machine, better way is to store this image relative to the project folder, like

    Try this.
    chetan dhumane
    Ranch Hand

    Joined: Jan 07, 2009
    Posts: 629

    What is l:\
    Sagar Rohankar
    Ranch Hand

    Joined: Feb 19, 2008
    Posts: 2902
        
        1

    typo



    chetan dhumane
    Ranch Hand

    Joined: Jan 07, 2009
    Posts: 629

    Sorry to say but images are displayed only on localhost
    Not on other machines
    Bauke Scholtz
    Ranch Hand

    Joined: Oct 08, 2006
    Posts: 2458
    You're doing it the wrong way. Above all, writing raw Java code in a JSP file is absolutely a bad practice. Never do that.

    You need a servlet which reads the InputStream of the image from the database and writes it to the OutputStream of the response.

    Then you invoke that servlet in the <img src>. You should not use absolute file system paths, that would only work if the server runs at the same machine as the client. Simply because of the fact that the client is the one which should request for images. If it encounters a file system path, then it will lookup the image in its disk file system. This isn't going to work if the image is located at the disk file system of the server machine.

    You can find here an example how to do it the right way: http://balusc.blogspot.com/2007/04/imageservlet.html (this article consits of 2 examples, you need to scroll down for the database example).
    chetan dhumane
    Ranch Hand

    Joined: Jan 07, 2009
    Posts: 629

    Thanks
    chetan dhumane
    Ranch Hand

    Joined: Jan 07, 2009
    Posts: 629

    But how that reuest.getPathInfo() is working.
    Means how do I call that Servlet from my JSP page what are the required arguments.
    Bauke Scholtz
    Ranch Hand

    Joined: Oct 08, 2006
    Posts: 2458
    Do not only copypaste the servlet code, but also read the article's text and the remnant code snippets and examples for web.xml and JSP.
    amit daundkar
    Ranch Hand

    Joined: Jan 24, 2009
    Posts: 47
    chetan dhumane wrote:I want to display images on my JSP page .I have images stored on my MySQL database , I am copying those images to c:\ ,the images get displayed correctly on localhost but when I run the same application on other machines images are not getting displayed.

    How to store those images as cookies in clients web browser to get it displayed in clients machine also.

    Thanks in advance.
    Chetan D.


    hello chetan,
    please give me that code to retrieve imgage. one that is working on localhost.
    Bauke Scholtz
    Ranch Hand

    Joined: Oct 08, 2006
    Posts: 2458
    Did you also read the responses on this topic?
     
     
    subject: Displaying images on JSP page