| Author |
FileNotFound in Servlet for Tomcat ? Help !!
|
jay vas
Ranch Hand
Joined: Aug 30, 2005
Posts: 407
|
|
Hi everyone : When Im running a web app in tomcat, I cant refer locally to a file i.e. FileUtilities.readFileContents("C:\\xsl\\clinicalInfo.xsl"); Does NOT work and results in a file not found exception. But On the other hand FileUtilities.readFileContents("C:\\Documents and Settings\\Administrator\\workspace\\CS\\xsl\\clinicalInfo.xsl"); Works just fine. How can I find out the directory in which the code is executing so that I can know what path to put when reading files ? Thanks Jay
|
 |
Bosun Bello
Ranch Hand
Joined: Nov 06, 2000
Posts: 1506
|
|
|
Check out the methods in the File class.
|
Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
You shouldn't rely on the current working directory when writing webapps. If the file is to be packaged with your webapp, use ServletContext.getResource or getResourceAsStream. Both methods will locate the file from a path relative to the context root. If not, use an absolute path. You can make this path configurable via a context or servlet init-param. Note: Depending on your needs, the ServletContext.getRealPath method may be a sufficient hack. I say hack because it's not really a portable solution. It will return null if called from within an app running from a packed war file. http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
Jaime M. Tovar
Ranch Hand
Joined: Mar 28, 2005
Posts: 133
|
|
|
I have a question here, i usually use classloader.getresourceasstream to load resources when working in a web app. Is this wrong???
|
She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
Originally posted by Jim Tovar: I have a question here, i usually use classloader.getresourceasstream to load resources when working in a web app. Is this wrong???
I can't say with any authority since I've never used the class loader to get resources in webapps. What I do know is that webservers often use custom classloaders for webapps (Tomcat does) and the behaviour of the class loaders (as far as getting static resources) is not rigidly defined in the servlet spec. It doesn't need to be since ServletContext has methods defined for doing this. Since it's not rigidly defined, I wouldn't feel comfortable distributing an app that uses it unless I've personally tested it on every container and container version (+ the various operating systems) that I indended to support. Out of curiosity: why did you choose to use the classLoader instead of the ServletContext for getting resources?
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56178
|
|
|
The getResource mechanism for the ServletContext locates resources relative to the context path. The class-loader getResource mechanism locates resources on the classpath. I've used the latter on many occasions to locate resources bundled in jar files.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
 |
|
|
subject: FileNotFound in Servlet for Tomcat ? Help !!
|
|
|