Insert image into jsp page

I'm sorry that I need to ask about this, but I've been trying to do this for three days now. I am creating a Java Java application and I want to include an image in a JSP page. The project name is realestates, and I have a Files folder inside the realestates folder.

My code looks like this:

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

This is what is created on the page after it is opened in the browser:

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

BUT, the image is not sent, only "govno" is recorded. I tried many different paths (relative, absolute, altered folder structure millions of times and all that I could think of and find on the Internet, but nothing helped). Who would say that such a thing would be so hard?

Folder structure on a Tomcat server after deployment:

 webapps - realestates |- WEB-INF |- Files |- kurac.jpg 
+7
source share
5 answers

Here's a guy explaining this in less than a minute.

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

So, two things are needed:

1. Add this line to some xml configuration file

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

2. Include the image in the JSP page using this line

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

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

THIS IS JUST AN EXAMPLE ... AND MAYBE ERROR BUT THIS IS THE WAY TO INSERT THE 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(); 
+1
source

It seems you yourself (and everyone else) are confused about where the image is. From your question, it seems to be located in webapps/realestates/Files/kurac.jpg , so this should work:

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

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

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

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

 <img src="/kurac.jpg"> 

If this is not the case, then something is wrong with your Tomcat configuration. Try reinstalling.

0
source

First you must create a folder with images outside the WEB-INF and try this code <img src="${pageContext.request.contextPath}/Files/kurac.jpg"/>

0
source

A web page never allows access to 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 in the browser).

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

Source: https://habr.com/ru/post/1268407/


All Articles