lijep dam
lijep dam

Reputation: 636

Insert image into jsp page

I am sorry that I need to ask this but I already spent three days trying to do this. I am buidling Java Web application and I want to include image to JSP page. Project name is realestates and I have Files folder inside realestates folder.

My code is like this:

<img alt="govno" src="<%=request.getContextPath() + "/Files/kurac.jpg"%>" style="width: 400px; height: 300px;">

This is what gets generated on page after I open it in browser:

<img alt="govno" src="/realestates/Files/kurac.jpg" style="width: 400px; height: 300px;">

BUT, image is not dispayed, only alt "govno" is written. I have tried many many paths (relative, absolute, changed folder structure million times and whatever I could think of and find on internet but nothing helped). Who would say that such a thing will be so hard to do???

Folder structure on Tomcat server after deployment is:

webapps
 - realestates
   |- WEB-INF
   |- Files
     |- kurac.jpg

Upvotes: 7

Views: 41809

Answers (5)

uday tondwal
uday tondwal

Reputation: 1

Webpage never allows accessing any local files.
This means that if you write img src="c:\imagesfolder\abc.jpg" in the jsp file it will not work (it can work only in some editor but it will not work using a browser).

img src="http://localhost.8080/imageshow/sendimage/12/abc.jpg" width="100" height="100"

Upvotes: 0

lijep dam
lijep dam

Reputation: 636

Here is a guy explaining it in less than a minute.

https://www.youtube.com/watch?v=dwjwSYOrnS8

So two things are required:

1.Add this line in some config xml file

<mvc:resources location="/files/" mapping="/files/**"></mvc:resources>

2.Include image into JSP page with this line

 <img src='<c:url value="/files/korali.jpg"></c:url>' />    

Upvotes: 7

Binod Pant
Binod Pant

Reputation: 332

At first you must create image folder outside the WEB-INF directory and try that code <img src="${pageContext.request.contextPath}/Files/kurac.jpg"/>

Upvotes: 2

Rei
Rei

Reputation: 6363

Looks like you got yourself (and everyone else) confused of where the image is. From your question, it seems to be at webapps/realestates/Files/kurac.jpg, so this should work:

<img src="/realestates/Files/kurac.jpg">

From your first comment, it is at C:/Users/Lazar/Documents/workspace-sts-3.8.3.RELEASE/realestates/Files/kurac.jpg, so it will not be accessible via http://. This won't work.

From your later comment, it is at /webapp/realestates/WEB-INF/Files/kurac.jpg. Files inside WEB-INF are not publicly accessible. This won't work either.

As a last resort, move the image file to webapps/ROOT directory. Try http://localhost/kurac.jpg from your browser. Replace localhost with your server hostname as necessary. If it works, this will work as well:

<img src="/kurac.jpg">

If it doesn't, there is something wrong with your Tomcat configuration. Try reinstalling.

Upvotes: 2

Yaksh
Yaksh

Reputation: 11

I read your question and i have one solution for your problem you can use the INPUT STREAM for add an image in JSP page...

THIS IS JUST EXAMPLE...AND MAY HAVE ERROR BUT THIS IS THE WAY HOW TO INSERT IMAGE IN JSP...

Class.forName("com.mysql.jdbc.Driver").newInstance();  
Connection connection = 
    DriverManager.getConnection(connectionURL, "user", "pass");  

psmnt = connection.prepareStatement(
    "insert into save_image(user, image) values(?,?)");  
psmnt.setString(1, username);  

ImageIO.write(image, "png", new File("C://image.png")); 
File imageFile = new File("C://image.png");
FileInputStream fis = new FileInputStream(imageFile);

psmnt.setBinaryStream(2, (InputStream)fis, (fis.length()));
int s = psmnt.executeUpdate();

Upvotes: 1

Related Questions