I'm not sure this question belongs in this area, but part of it is definitely Servlet-related.
I'm using a custom tag library/page builder servlet to put together my jsp pages. As I understand it, the Servlet gets loaded on startup because it is included within a JSP library. The Servlet goes to the web.xml to find the location of the template that I want to use as a basis for my jsp pages. The Servlet then locates and reads in the file and does some processing. That's all perfect.
My issue is that I can only get this to work if I put the absolute path to the template file in the web.xml. My new supervisor wants me to use a relative path in the web.xml. I have tried putting the template.inc file in various places and using a relative path, but it isn't found using any of these methods. I wasn't sure if it should be relative to the web.xml file, relative to the location of the tag library jar file, or relative to a page that uses the tag library. None worked.
I have the same problem when I use FOP. Within the Java class, I need to locate the .xslt file on the server and it only works when that location is hard-coded.
Is this just the way it is? Is there a reference where I can look it up and show it to my tech lead?
Using relative paths are better in my opinion too.I mean relative to the application root.
say I put the following in a servler config param in the DD of my web application.
param = fileName and value is something path like ,relative to the web application like /WEB-INF/myTemplete.inc
This is relative to my webapplication.Now to read this resource we can use servletContext.getResourceAsStream(theLocation) [theLocation we can get from servletConfig.getParameter("fileName)] or servletContext.getResourceAsStream("/WEB-INF/myTemplete.inc")
One thing that you, and your supervisor, should keep in mind is that a Java web app doesn't have to be deployed as a part of the machine's file system. It can be deployed as a packed war file.
When deployed this way, there is no "relative path" to anything because the webapp isn't part of the file system. Apps that rely on a particular type of deployment are said to be less portable.
If the file you're trying to access is always to be located under the application's directory structure, then you should use ServletContext.getResource or getResourceAsStream. These methods take a path that is relative to the root of your web application and work the same way, no matter how your app is deployed.
If the resource that you need to reference exists outside of your webapp, then you would be better of using an absolute pathname. You can make this path configurable as either a context-init-param or as a servlet-init-param.
All that being said... If you know that you will always be deploying your app from an exploded file system, the method ServletContext.getRealPath("/") will return the absolute path to the root of your webapp. You can use this to dynamically build the full path to your files. This will allow you to use relative paths in your code. [ March 19, 2007: Message edited by: Ben Souther ]