I've been too embarrassed to post a request for help here, because I was sure that my problem is so elementary that it must be explained somewhere in kindergarten-level Servlets, but after three days of (discontinuous) searching the Web and two books, I still can't find an answer. Here's the problem: in the early stages of setting up the model code, I need to read an XML file. Try as I may, I cannot figure out how to get the servlet to read the file: everything I try returns the same FileNotFound Exception.
Programming context: Mac OS 10.7.5, Eclipse, using localhost
Here are some of the things I have tried:
Referencing the file by its name only:
Putting the file into WEB-INF
Putting the file into WebContent
Putting the file into the servlet directory
Referencing the file by its absolute position in the directory structure, as in "Drive/Users/me/Documents/workspace/projectName/WebContent/FileName"
Referencing the file by the ContextPath obtained by request.getContextPath()
Referencing the file by the FilePath obtained by request.getContextPath().getFilePath(). I wrote this to the console to make sure and got this filePath:
to which I appended "WebContent/FileName".
Nope, it didn't work.
So, I know that I need to get a filePath relative to the servlet. Apparently request.getContextPath().getFilePath() doesn't work.
Any suggestions, as in "Did you remember to plug in the computer?"
If you need to read a file that resides on the same machine as where your webapp is running, you shouldn't use request.getCOntextPath. You will need the complete path to the file, and you will need to open it just like you were opening the file in an plain old Java program
This is my third attempt at responding to your suggestions. The first two times, it didn't work, but while composing the statement of what didn't work, I came up with a possible question you might ask, and in checking it out, I got closer to the answer. Now I have something that sorta works. It's got a weird problem, but I'm not sure it's really the same as the original question. I'll continue poking at this thing and will either a) come back here to announce that I got it right; or b) post a new problem. But I won't waste your time until I've done my homework.
I don't know what you have done, but here's a suggestion: If your solution depends on the fact that you're using Eclipse (e.g. you mentioned some Eclipse directories in the path) then it's not the right solution. In real life you would only be using Eclipse to develop the servlet application, and then you would run it in a completely different environment which doesn't use Eclipse at all.
In many posts people on the site with extensive servlet experience have talked about working outside an IDE. Bear mentioned it on your post last weekend. I have moved from Netbeans to Eclipse because Netbeans had problems compiling Bear's Frontman class. Now I am having problems with both of them making connections to an older MS-SQLserver because it needs an outdated jar to connect to the databases.
To tell the truth, I've downloaded the Java EE version of Eclipse and I'm using it. I had a plan to convert my Swing application to a web app, and probably some day I'll get around to that, but right now I'm not using it for web applications.
However the same thing applies to plain old Java applications which you develop in Eclipse -- use the classpath to find files which are associated with your application rather than hard-coding Eclipse directory names.
I'm having trouble understanding how this file is to be used. I have servlets that read files from a fixed location (config files), have files that are sent to the browser (icons, css, js), and files that are used as resources (templates).
They all use different ways to access them. BTW I use NetBeans and develop and test on Mac and Linux, deploy on Linux.
Perhaps if you tell us how you want to use this file we can be more clear on the methods available.
In short, for config files that I want the admin to edit, I put it in a "standard" location like /usr/local/<program name>/etc and open in the regular java way:
where configFile is a File object.
For stuff the browser gets I put the files in a Folder call Web Pages, same place the jsp's go (not in WEB-INF)
For resources they have their own folder called resources and i use the method Bear recommended.
It's not what your program can do, it's what your users do with the program.
Joe, it's an XML file containing data used by the model, which is a complex simulation. I also have lots of image files that need to read at various times. As I mentioned earlier, it's trivial to access it in the standalone app using the standard File system and System.getProperty("user.dir"), but that obviously won't work in a servlet.
When you say it's data for your simulation, do you mean that it is part of the application and is only changed by a programmer, or is it something the user uploads depending on what they want to simulate, or something in between where the user doesn't change it but a sysadmin with XML skills but not necessarily Java skills might change?
If it's really part of the program and only you or someone else maintaining the program changes, I suggest using a resource as it then becomes part of the war file.
If the user uploads it get puts in a temp directory and you deal with that way.
If it's something a sysadmin may change, then I suggest some sort of "config" directory in a fixed location. I use /usr/local/<myProgramName>/etc but any fully qualified path that is readable by Tomcat works.
For the image files, my question is can we consider them static content or do you manipulate them somehow in the servlet? IOW do you want a <a> tag to the image as is or do you have a url that calls some servlet to read modify and send them?
I tend to do both things. For the ones that are static they should go in the top level of the war file and are addressed as /<image file>.jpg in the <a> tag.
For the ones I manipulate or include in creating other images, I tend to put them in a mysql database but there's no reason they can't be a resource or in a fixed location (like the xml file).
Sorry, Joe, I should have specified up front that all these files are static. Read-only. I'm the only person who'll fiddle with them, and in truth they're already finished and are, as far as I'm concerned, cast in stone. I'll check out that StackOverflow thread. In my many searches for solutions, Google keeps giving me links to StackOverflow at the top of its list.