This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am working on a servlet that within JBuilder can access a txt file that I read in and parse for data. When I create my .WAR file and put this on my server running Tomcat 4.0 the txt file not only ins't included in the .WAR, if I place a copy of the file on the server it still cannot be accessed. Is there a way that I do this with a servlet? If so where would I need to put the file so that the server can find it and is there a way to get it into the .WAR file? I am just wanting to be able to manage a single txt file on the server in a known location (preferably where the servlet is installed) that I can read in and modify. Any ideas?
How are you trying to access your text file? You can use ServletContext.getRealPath() to place a text file somewhere within your web application itself and interactwith it as a file... however, this is not a smart thing to do when you're deploying as a WAR file... I believe it will work on Tomcat, but it may not work elsewhere. What we do in a similar situation is to provide a servlet context config parameter that specifies a directory that we use to store files. In our case, it's more than a single text file... but you could do the same thing. Put the text file in any accessible place on the server, and put the path to find it in a servlet context config parameter in your web.xml or the servlet container's global config file. Then it's up to your deployment engineers (which may be you again) to decide where they want it. Chris
Joined: May 24, 2001
Thanks Chris, When I run my servlet in JBuilder I am able to access the file by using the File class. I know which directory the application is running in using "user.dir" and appending the path to that for where the file is. I have never tried this with a servlet until now. My main concern was getting the file itself into the deployment by getting it into the WAR file, but if I can't access the file even when it is there, getting it in the WAR file is a moot point. I am writing a servlet and web application to allow end-users access to a device we are designing and the only front-end to the device will be the web application. Tomcat is the server and Linux is the OS. I am trying to make the case that using servlets and JSP will be sufficient and creating a demo to prove that. Thanks for the suggestions. I will look into how to implement them. Lon
Put the .txt file in the WAR. The best place is in the same directory as the web.xml file. Like the web.xml file, you can edit it and redeploy at any time. From within the servlet, use the ServletContext.getResourceAsStream method. This method requires the name of the file from with that context. If the file was called info.text, you would use sc.getResourceAsStream("/WEB-INF/info.txt"). The getRealPath method may not work if you deploy as a .war file, but the getResourceAsStream method is something I am using from within a .war file(Log4j.properties file using WebLogic). Good luck.