This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hi! I think what I'm trying to do is an absolute url....I'm not sure! In my application, I read variables on a properties file. Right now, the path to the file is hard-coded (ie e:\sites\.....) I need to change that so that I can read the properties file no matter where my servlet is. I tried using the same format as when I call another servlet from an html page (form action="http://keoki.ncr.pwgsc.gc.ca:8080/....") but that doesn't seem to work. What is the correct way to write the url? Thanks! Annette
Really? So if the application gets moved to another server, we would have to move the properties file and then chance the path in every servlet that calls it? (just need to get my case together so I can tell them it's not possible!!)
Originally posted by Annette L'Heureux: In my application, I read variables on a properties file. Right now, the path to the file is hard-coded (ie e:\sites\.....)
Eeeks! Hardcoded absolute paths! There are a couple of ways you can get at resources. The most appropriate is probably ServletContext.getResourceAsStream(). This allows you to open an InputStream on a resource file. This is relative to the webapp-root, but does not go through the web server and can access privileged directories such as WEB-INF. For example, if your web-app is in /my/directory/myapp/ then getResourceAsStream("/WEB-INF/myresource.file") will open /my/directory/myapp/WEB-INF/myresource.file Alternatively there's the getResource() method giving back a URL rather than an InputStream. You can get the servlet context object using Servlet.getServletConfig().getServletContext(). If you would rather retrieve stuff from the CLASSPATH, you can use either getClass().getResourceAsStream() or ClassLoader.getSystemResourceAsStream(). But I don't think that is quite as portable or convenient. HTH - Peter
[This message has been edited by Peter den Haan (edited March 02, 2001).]