Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert image into jsp page

Tags:

html

jsp

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
like image 992
lijep dam Avatar asked May 31 '17 15:05

lijep dam


4 Answers

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>' />    
like image 179
lijep dam Avatar answered Oct 21 '22 09:10

lijep dam


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.

like image 30
Rei Avatar answered Oct 21 '22 09:10

Rei


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();
like image 45
Yaksh Avatar answered Oct 21 '22 07:10

Yaksh


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

like image 31
Binod Pant Avatar answered Oct 21 '22 09:10

Binod Pant