This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I'll try to give as much information as possible to make this easiest. I added some errata info at the bottom. The problem: I cannot get my servlet to correctly read and write files. I tried just using straight out file paths, but I ran into the system-dependent/container problem of what the actual path is. To combat this, I attempted to use getContext().getRealPath(String x), but I have been having problems with that. I am attempting to write to a file, and if it is not present, then I must create it. I heard that getRealPath will return null if the file in question does not exist or if there is a problem determining its location. In my case, getRealPath always seems to return null, even when looking for simple things. I decided to test it and used getRealPath("/index.html") and even this returned null, although I am sure the file is there. All I need to know is how to get to my file! I am taking input from an HTML form, then putting it into a List then writing the list to a file. Each time someone new submits the form, my program reads the list from the file, adds a new node, then writes it back out. At the end of the week, I use another application to read the file and see who guessed correctly. If there is a better way to do this, please let me know! Also, I do not have access to any database tools nor do I have any DB programming experience. Besides, I will never have more than 500 entries in my list, so hardcore database stuff isn't necessary. Notes: First, I'm using JRun 2.3 running on a Macintosh WebSTAR 4.2 I don't know anything about either of those except JRun is nowhere near as good as Tomcat, and WebSTAR sucks compared to Apache. Second, I can't install Tomcat and I can't get another server. As much as I'd like to, it's not my equipment, it's my boss's. Lastly, my servlet is self-contained in a .jar file. I have heard of .war files, but I have not been able to find much information about them. I mention this because 1) I could not get JRun to run my servlet when it was extracted and 2) I read that getRealPath() gets weird when used with a .war
Try to do this File anyFile = new File("/index.html"); Then print the path anyFile.getAbsolutePath() You will notice that the path here may not correspond to the root of your webapp. Instead it may be a different location (may be the location of JRun server startup location..) This might give you a clue why your file i/o is not finding the file..!! ------------------ _________________________ Rama Raghavan SCJP2