aspose file tools*
The moose likes JSP and the fly likes how to retrieve image in jsp page from a seperate folder. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "how to retrieve image in jsp page from a seperate folder." Watch "how to retrieve image in jsp page from a seperate folder." New topic
Author

how to retrieve image in jsp page from a seperate folder.

j u ray
Greenhorn

Joined: Sep 21, 2009
Posts: 9
hello everybody,
I am in an acute problem, i will be grateful if anyone can help me.

i need to display image in the ".jsp" page. i have created a folder called "image" in the web folder of my project. in the image folder i am storing all the image.

in my database there is one column "ma_image", here i am stroring the filename of the image, say in my "image" folder the image name is "house.bmp" and in the database i am storing "house.bmp" in the "ma_image" column. i am just typing the name of image along with the full extension, i am not using any OLE object to store the image.

now i want to display the image after retrieving it from the "image" folder of my project. the location of "image" folder is:- E:\HomeJsp\web\image

my code is like this::

String path="image/";
//String path="E:/HomeJsp/web/image/";

out.println("<tr><td class=\"menu_tdtd\">Image: </td> <td class=\"menu_td\" id='macImage"+f+"'> </td></tr>");
// out.println("image displayed:-:"+path+"msg");

But i am not getting the image. my output is the filename of the image like-- "home.jsp" it is written in the <td> tag of the browser.

please get me a solution to this problem as soon as possible.

Thanks in advance.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

You are using out.println() is a JSP to emit HTML?

Firstly, there should be no Java code in a JSP at all. And the whole purpose of a JSP is to server as template for HTML, so putting the HTML inside a string, inside Java, is complete inside-out.

You really need to update your JSP knowledge.

That said, you cannot use a file path to reference your image. The file path only makes sense on the server machine and has no meaning once the HTML has been served to a client.

You need to either:
  • Put the image file somewhere that it can be served via a URL.
  • Write a servlet that can read the file from disk and stream it as a response.


  • [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    jas preet
    Ranch Hand

    Joined: Mar 31, 2010
    Posts: 78

    You need to either:
    # Put the image file somewhere that it can be served via a URL.
    # Write a servlet that can read the file from disk and stream it as a response.


    Wht to write a servlet code for this ??? i think the jsp itself gets converted to servlet..so why write a seperate servlet code for this..


    Thanks Jaspreet ----- Nothing is permanent
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61648
        
      67

    jas preet wrote: i think the jsp itself gets converted to servlet..so why write a seperate servlet code for this..

    What happens to the JSP is completely irrelevant. It's the URL of the <img> tag that matters.

    And if the image is not addressable by URL, then the way to fetch it is to have a servlet (that does have a URL) fetch and stream the image as its response.

    The easiest solution is, of course, to simply move the image to somewhere that it can be served by the server.
     
    wood burning stoves
     
    subject: how to retrieve image in jsp page from a seperate folder.