You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jacob Kjome <ho...@visi.com> on 2002/06/30 17:48:22 UTC

Re: [Offtopic] Setting the directory where IO occurs in a servlet?

first of all, if WEB-INF/files is where you *always* want to write stuff, 
why not do something more portable like this:

<servlet>
         <servlet-name>parts</servlet-name>
         <servlet-class>PartsDepotServlet</servlet-class>
         <init-param>
             <param-name>physicalFilePath</param-name>
             <param-value>WEB-INF/files</param-value>
         </init-param>
</servlet>

Then, in the init() of your servlet, do:

String contextPath = getServletContext().getRealPath("/");
String filePath = getInitParameter"physicalFilePath");
String systemFilePath = filePath.replace('/', File.separatorChar);
File log4jFile = new File(contextPath+systemFilePath);

Now you never have to touch your web.xml again no matter what system you 
run on and no matter where your app actually exists on that system.


Second of all, be very careful when you do this.  You cannot assume that 
you will be able to write to the WEB-INF directory of your webapp.  If you 
run directly from a .war file, File IO will be impossible to that area 
because it will exist in an archive, not on the file system. So, just make 
sure you are running your app out of a directory.

As for file IO, you could set a system property with the path generated by 
the code above and use that system property when doing File IO.  I'm not 
sure how it would be done automatically, but I think it is possible since a 
lot of IDE's do a similar thing.


Jake



At 09:02 AM 6/30/2002 -0400, you wrote:
>Hi Folks,
>
>I have a servlet, running in Tomcat 4.0.3, which reads in files and
>writes files.  Currently it does the reading and writing to the folder
>where Tomcat was started.  I would like to have control over where the
>servlet reads and writes.
>
>My plan for controlling where IO occurs is to incorporate into my
>webapps web.xml file the location of where I want IO to occur:
>
>     <servlet>
>         <servlet-name>parts</servlet-name>
>         <servlet-class>PartsDepotServlet</servlet-class>
>         <init-param>
>             <param-name>physicalFilePath</param-name>
>
><param-value>c:\tomcat\webapps\parts-depot\WEB-INF\files</param-value>
>         </init-param>
>     </servlet>
>
>Then, in my servlet I will read in the path:
>
>     physicalFilePath = getInitParameter("physicalFilePath");
>
>At the point I would like to somehow express "from now on all IO done by
>the servlet should go to the folder indicated by physicalFilePath".  Is
>there some way to do that, either in the Java API or in the Servlet API?
>
>Is this strategy that I am taking the best approach?  /Roger
>
>P.S. I realize that this is not really the appropriate list for this
>question.  What list do you recommend?
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>