2

Right now I am trying to accomplish a project on Eclipse Juno and Tomcat 7 that requires to have a "virtual folder" to hold multimedia files (like images, other sub-pages,etc.). I already have some methods to give out the file path in a URI based syntax (lets say I want to access images in /Content/Image) and I want to map that URI to C:\Users\MyUser\Content\image (I am aware that I am binding the project to Windows systems but I will workaround later on in this issue).

Currently my project is called pj, and Eclipse created a context called pj inside the eclipse's tomcat instance (and thats makes a lot of sense). When i test my project with

> http://localhost:8080/pj

it works fine (and it's supposed to).

But there is a problem here: until now I haven't found a way to create a URI in tomcat to actually go to the Content/Image path to grab content to add to my pages (read somewhere that is unhealthy to keep content on WEB-INF folder, so i'm trying to actually get it done the right way). Also read somewhere that to accomplish this objective, I have to do something like this in the contexts:

<context docbase="d:/images" path="/Content/Images"></context>

Also read there that in tomcat, to resolve URIs you have to use contexts to achieve that goal (giving a bridge between the meaning of he URI and it's location in the file system). Still, as from tomcat 4 (if not mistaken) it is not supposed to fiddle around server.xml, so in ANOTHER attempt to make this right, i try to actually add a context in META-INF inside context.xml with the code shown before. But there is here ANOTHER problem! It seems that adding the path tag makes tomcat go nuts, as said here: http://tomcat.apache.org/tomcat-7.0-doc/config/context.html .

So I am really in a bind here.... What I want to ask is:

  1. What is the best way to actually add an external folder in a web project to fetch multimedia content and
  2. How it is supposed to make it work inside Eclipse?

PS: I am asking this because in one of my methods inside my project I am using the getLoader method to return the InputStream (java.io InputStream NOT Corba) and it return nulls (which means it doesnt find it).

EDIT: Tried to actually fiddle around server.xml by inserting the conext by hand but didn't work, inserting the relative URI doesn't work on the server (local:8080/Content/Image with valid files inside) or going inside my main project and do getstream doesnt work too

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
John
  • 311
  • 4
  • 17
  • If you need to get at the content programmatically, it seems more logical to me to store it in the WEB-INF folder. – wds Sep 12 '12 at 12:30
  • That is true, still I read somewhere that storing things on WEB-INF is not a good approach, since that requires tomcat to explode the webapp, and that is not very reliable. And in a test I realized, tried to access things on WEB-INF and for some reason I couldn't retrieve a JPG file (but I can retrieve a .properties file...) – John Sep 12 '12 at 13:45
  • 1
    This might be relevant: http://stackoverflow.com/questions/5915425/programmatically-reading-static-resources-from-my-java-webapp – wds Sep 13 '12 at 10:21
  • I am really gratefull you shared this question! It seems thats classLoader cannot retrieve files outside the WEB-INF and that can be a cause for this mess... I will work on this today and I'll post a detail problem/solution post describing it if I can reach a solution – John Sep 13 '12 at 10:34

2 Answers2

2

After some fiddling around, tweaking, etc. I came up with a workaround for this situation. Like I stated, it IS possible to actually have an outside folder hold all the multimedia and/or pages as you wish. One of the references to that solution is here: http://harkiran-howtos.blogspot.pt/2009/08/map-external-directory-into-your.html .

Still, for some reason, this is not quite possible to make it work inside Eclipse (or I have failed something and wasn´t unable to make it work). But there is an alternate solution for this. It is also feasible to actually have a folder for that purpose INSIDE the web app but OUTSIDE the WEB-INF and META-INF folder. In other words, a folder that is located in the ROOT of the web app.To access those files in that folder you can use something called ServletContext. That context has actually inside all possible references to the folder structure of your web app. To access those files with the context give, you have to use getResourceAsStream from the Servlet context (or use getRealPath if it is necesary and/or you can guarantee that the web app is exploded inside Tomcat). So in other words, to access folders inside the web app but outside the WEB-INF and META-INF you have to use ServletContext and their given methods to get files/streams.

PS: Ty wds for pointing out ServletContext

John
  • 311
  • 4
  • 17
0

I made the harkiran's solution work but it's not very good solution.

People discourage use of getRealPath. Mapping external folder is good thing to do for many reasons.

But to do it in Eclipse, you need to go to deployment folder. In my case it's hidden folder inside Eclipse workspace.

workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/

Inside that folder you shoud make directory structure and file from harkiran's solution. I works until you delete and recreate server in Eclipse.

After that you need to make it again.

Marko
  • 1,267
  • 1
  • 16
  • 25