Relative paths in servlets?
Hi, could anyone tell me how to access files using relative paths from a servlet? Say, if servlet is mapped to /bar and the file foo.html is in the webapps top dir. How do I access the foo.html file from the servlet. I tried ../foo.html, /foo.html and even getServletContext().getRealPath(/bar)+/../foo.html but none works. Any pointers? -- Øyvind Hvamstad -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
RE: Relative paths in servlets?
Hi, Did you try getServletContext().getResourceAsStream(/foo.html); ? Yoav Shapira Millennium ChemInformatics -Original Message- From: Øyvind Hvamstad [mailto:[EMAIL PROTECTED]] Sent: Monday, January 06, 2003 8:36 AM To: [EMAIL PROTECTED] Subject: Relative paths in servlets? Hi, could anyone tell me how to access files using relative paths from a servlet? Say, if servlet is mapped to /bar and the file foo.html is in the webapps top dir. How do I access the foo.html file from the servlet. I tried ../foo.html, /foo.html and even getServletContext().getRealPath(/bar)+/../foo.html but none works. Any pointers? -- Øyvind Hvamstad -- To unsubscribe, e-mail: mailto:tomcat-user- [EMAIL PROTECTED] For additional commands, e-mail: mailto:tomcat-user- [EMAIL PROTECTED] -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
Re: Relative paths in servlets?
/bar is your servlet mapping which has nothing to do with a real path on your system. You want to get the real path from the base of the context: String contextPath = getServletContext.getRealPath(/); Check is that is null before using it as it *will* be null if you serve your app directly from a .war file rather than from a directory on the file system. Now do contextPath+foo.html and create a File object from that. Jake At 02:35 PM 1/6/2003 +0100, you wrote: Hi, could anyone tell me how to access files using relative paths from a servlet? Say, if servlet is mapped to /bar and the file foo.html is in the webapps top dir. How do I access the foo.html file from the servlet. I tried ../foo.html, /foo.html and even getServletContext().getRealPath(/bar)+/../foo.html but none works. Any pointers? -- Øyvind Hvamstad -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
Re: Relative paths in servlets?
On Mon, 2003-01-06 at 15:22, Jacob Kjome wrote: Check is that is null before using it as it *will* be null if you serve your app directly from a .war file rather than from a directory on the file system. Odd, I do serve mine from a .war file, however the contextPath is not null, but points to the build directory. This is probably not true if the war is buildt elsewhere than on the local host. Jake Thanks Øyvind -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
Re: Relative paths in servlets?
You are confusing the WAR structure with the actual .war archive. The .war archive is a .jar format archive. I challenge you to try to use File IO to access a file inside a .jar or .war archive. If you can do it, please let us all know because you will have broken new ground in Java :-) Remember that Tomcat auto-expands .war files to a directory structure and then deploys the directory structure. Also, you mention that your contextPath points to the build *directory*. That is, obviously, not a .war file archive, but a directory structure which will, quite obviously, work to get a real path using File IO. The only way to test serving out of a .war archive in Tomcat is to define a Context element with the docBase pointing to a .war archive. For instance... Assuming the .war file is in CATALINA_HOME/webapps Context path=/mypath docBase=myWarArchive.war/ Make sure to stop tomcat before you put the .war file into webapps. Add the above to your server.xml or a context configuration file (like admin.xml or manager.xml are set up). Then put the .war file into webapps. Now start Tomcat. Then, try to access a servlet which uses getRealPath(/). Check the value. I guarantee it is null. Jake At 03:23 PM 1/6/2003 +0100, you wrote: On Mon, 2003-01-06 at 15:22, Jacob Kjome wrote: Check is that is null before using it as it *will* be null if you serve your app directly from a .war file rather than from a directory on the file system. Odd, I do serve mine from a .war file, however the contextPath is not null, but points to the build directory. This is probably not true if the war is buildt elsewhere than on the local host. Jake Thanks Øyvind -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
Re: Relative paths in servlets?
On Mon, 2003-01-06 at 16:31, Jacob Kjome wrote: Assuming the .war file is in CATALINA_HOME/webapps Context path=/mypath docBase=myWarArchive.war/ Make sure to stop tomcat before you put the .war file into webapps. Add the above to your server.xml or a context configuration file (like admin.xml or manager.xml are set up). Then put the .war file into webapps. Now start Tomcat. Then, try to access a servlet which uses getRealPath(/). Check the value. I guarantee it is null. I am using the ant installTask, I was somehow confused to believe that it used .war, but that was a mistake. No need to be agrevated. snip from=build.xml target name=install depends=dist description=Install application to servlet container install url=${manager.url} username=${manager.username} password=${manager.password} path=${app.path} !-- THIS PROBABLY THREW ME OFF -- war=file://${build.home}/ /target /snip Jake Thanks Øyvind -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
Re[2]: Relative paths in servlets?
Hello Øyvind, I'm not aggravated, I was just pointing out the misconception. Jake Monday, January 06, 2003, 9:30:26 AM, you wrote: ØH On Mon, 2003-01-06 at 16:31, Jacob Kjome wrote: Assuming the .war file is in CATALINA_HOME/webapps Context path=/mypath docBase=myWarArchive.war/ Make sure to stop tomcat before you put the .war file into webapps. Add the above to your server.xml or a context configuration file (like admin.xml or manager.xml are set up). Then put the .war file into webapps. Now start Tomcat. Then, try to access a servlet which uses getRealPath(/). Check the value. I guarantee it is null. ØH I am using the ant installTask, I was somehow confused to believe that ØH it used .war, but that was a mistake. No need to be agrevated. ØH snip from=build.xml ØH target name=install ØH depends=dist ØH description=Install application to servlet container ØH install url=${manager.url} ØH username=${manager.username} ØH password=${manager.password} ØH path=${app.path} ØH !-- THIS PROBABLY THREW ME OFF -- ØH war=file://${build.home}/ ØH /target ØH /snip Jake ØH Thanks ØH Øyvind ØH -- ØH To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] ØH For additional commands, e-mail: mailto:[EMAIL PROTECTED] -- Best regards, Jacobmailto:[EMAIL PROTECTED] -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]