This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
As far as I found out, I have the option to place the files:
- in the root directory of the web-app, where they are visible to everyone
- in the WEB-INF/classes folder where they are visible only to the application
I wish to place the my XML encoded files somewhere hidden from users but I don't believe that placing them together with the java class files is that good...
First, just to make one thing clear: you should NEVER write files to a WAR. It not only violates the Java standards, it can cause serious pain and suffering. So any files in a WAR should be read-only.
If you want a file to be inaccessible from URL (client) requests, you have to put it somewhere under WEB-INF, because that's the only place that resources are invisible to clients. Anywhere outside of there could be secured, but it would still be visible, so it's better if you just put it under WEB-INF. Come to think of it, META-INF might work, but I don't recommend it. Use WEB-INF.
WEB-INF is a general directory and only the "classes" and "lib" subdirectories under it are actually class resources unless you have customized classloading. If you created a file named /WEB-INF/data/POIList.xml, the classloader wouldn't be able to find it. However, an HttpServeletRequest . getResourceAsStream("/WEB-INF/data/POIList") would work wonderfully as a feed to your favorite XML parser. The Request getResource methods can access any resource in a WAR, not just classpath resources.
Customer surveys are for companies who didn't pay proper attention to begin with.
Your getResourceAsStream failed because it's an instance method, not a class method. You need the actual HttpServletRequest object, not its class.
If you need to upload or otherwise maintain mutable files related to a web application, you should designate an external location outside of the web application (and the Tomcat server) to hold them. A database may or may not be useful here, depending on your needs.
Conventionally in Linux, you would store file like that in a directory named something like /var/lib/myapp/uploads. In Windows, there's no convention, so just make a directory somewhere. I always make the directory location a variable (resource-reference) so that it isn't hard-coded into the webapp.
Joined: Mar 08, 2011
Tim Holloway wrote:Your getResourceAsStream failed because it's an instance method, not a class method. You need the actual HttpServletRequest object, not its class.
I forgot to mention that one
The HttpServletRequest Object "request" that is passed to the Servlet in the doGet, for example, does not have a getResource or getResourcesAsStream method. Only If one calls getClass() but that goes over the classLoader so this does not work
Am I missing something?
And thanks to the other repliers, will keep the tips in mind
Are you missing something? Only that I'm too lazy to RTFM.
It's on the ServletContext, not the HttpServletRequest. I'm fairly sure there's a path from one to the other, but I can't recall what it is at the moment. In any event, the Servlet itself has a getServletContext() method that can be used to get there.