Bishan
Bishan

Reputation: 15702

Display Image on web page using Java

In my Java EE app, there is a function to upload images. When uploading an image, I'm saving image path in MySQL database.

Now I want to display uploaded image on a web page using image path that saved when uploading the image in my MySQL database table.

How could I do this ?

Upvotes: 2

Views: 2697

Answers (2)

Ravinder Reddy
Ravinder Reddy

Reputation: 23982

Write a servlet that extracts your stored image from database and writes back into servlet outputstream.
You need to set the relevant mime type of the image, for example "image/jpeg", before writing into outputstream.

You need to point the image source to this servlet url with required input parameters to load the correct image from the database. For example :

<img src="http://mydomain/servlet/imageServlet?imgid=xyz" />

There is nice example given by BalusC at: ImageServlet serving from database

Upvotes: 3

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81674

It depends. Is the path someplace that a web server is serving documents from? Then just include the path, adjusted as necessary, in an IMG tag. If not, or if the image data itself is actually in the database (you're not entirely clear about this) then create a servlet which returns the contents of an image based on query parameters, and use that servlet's URL (plus the query parameters) in the SRC attribute of the IMG tag.

Upvotes: 3

Related Questions