2

i am saving my images through java coding in a folder called "files". and in my jsp page i am trying to display that image using <img alt="" src="${pageContext.request.contextPath}/files/IMG168.jpg"> But the image is not displaying! What could be the reason.

Please see the below images and suggest me solution for my problem.

enter image description here

enter image description here

Dan
  • 2,086
  • 11
  • 71
  • 137
  • pageContext.request.contextPath will direct you to webcontent. SO essentially your path becomes /webcontent/files/yourImage. But your files folder is not inside webcontent , hence the problem . – The Dark Knight Dec 17 '12 at 09:25
  • @TheDarkKnight If i put the `files` folder in `WebContent` directory. then ,if i redeploy my project then that file will be erased? I do not want it to be erased even if i redploye my project. – Dan Dec 17 '12 at 09:28
  • @AshutoshSingh : no it won't erase – Bhavik Shah Dec 17 '12 at 09:50
  • @BhavikShah are you sure? If it wont erased then my problem is solved. I will put all my project inside this folder`webconted/files` and access them using `${pageContext.request.contextPath}/files/IMG168.jpg` Is this the correct approach of uploading file into the server? – Dan Dec 17 '12 at 09:55
  • @AshutoshSingh: Store your files outside of the container then they would not be erased on re-deploy. – Aleksandr M Dec 17 '12 at 10:01
  • @AleksandrM if i put the above `files` folder inside of `WebCOntent` directory then they would not be erased on re-deploy?Is it correct or not? – Dan Dec 17 '12 at 10:06
  • @AshutoshSingh: If you are saving files programmatically inside project folder then yes they will be erased on re-deploy. – Aleksandr M Dec 17 '12 at 10:10

3 Answers3

2

You should put the files folder in WebContent directory.

Stanley
  • 5,057
  • 4
  • 34
  • 44
  • If i put the `files` folder in `WebContent` directory. then ,if i redeploy my project then that file will be erased? I do not want it to be erased even if i redploye my project. – Dan Dec 17 '12 at 09:27
  • @AshutoshSingh : are you asking whether that the file will be erased or telling that the file will be erased – Bhavik Shah Dec 17 '12 at 09:49
  • @BhavikShah I am asking whether that the file will be erased if i keep it inside the webContent folder – Dan Dec 17 '12 at 09:57
  • @AshutoshSingh, why would you worry whether the file will be deleted? Are those images in the directory uploaded by users via the application? – Stanley Dec 17 '12 at 12:19
  • @StanleyW. i am worry because. If users add their images then after few months if i make any changes in my software then i have to redeploy it in my server from this my old images will be deleted from the server thats why i was asking. which is the best place to store images? – Dan Dec 17 '12 at 12:26
  • @AshutoshSingh in that case you have to store the images in a location outside of your web application. The images can't be accessed directly by remote clients. So, you need a servlet to serve the image. Check out these similar questions: 1. [Java Web App upload and load images](http://stackoverflow.com/questions/11665999/java-web-app-upload-and-load-images) 2. [In java web application, where should i store users photos?](http://stackoverflow.com/questions/5293085/in-java-web-application-where-should-i-store-users-photos) – Stanley Dec 18 '12 at 02:13
1

Web pages contains as below

Web pages 
|______page1.jsp 
|______index.jsp 
|______page2.jsp 
|______WEB-INF 
|______images
|______css
|
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37
0

Just try this and see

<img alt="" src="../../files/IMG168.jpg">

Updated,

I think you have to get physical location of files folder and put it in to src. Example if your files folder in C:\visionbook\files your

src="C:\visionbook\files\IMG168.jpg"
someone
  • 6,577
  • 7
  • 37
  • 60
  • I think you have to get physical location of files folder and put it in to src. Example if your files folder in C:\visionbook\files your src="C:\visionbook\files\IMG168.jpg" – someone Dec 17 '12 at 09:57
  • i have one doubt. **If i put the `files` folder in WebContent directory. if i redeploy my project then that file will be erased or Not ? I do not want it to be erased even if i redploye my project** – Dan Dec 17 '12 at 09:59
  • When you redeploy the application while it is in server. That will not remove. If you are going to deploy newly built war file you may miss what ever the images were in your previous war. If you can make sure to insert previous images to new war file also. You can have your files folder under webcontent. Otherwise you have to physically locate as I have suggested. – someone Dec 17 '12 at 10:05