tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Shapira, Yoav" <Yoav.Shap...@mpi.com>
Subject RE: How to connect to a Data-File above the WEB-INF-Folder from a servlet????
Date Tue, 03 Dec 2002 15:22:19 GMT
Howdy,

>How do I connect to a Data-File above the WEB-INF-Folder from a
servlet????
>For example my class is placed in
>$CATALINA_HOME/webapps/ROOT/WEB-INF/classes/myApp/server/servlet.class
>and my data-files are placed in $CATALINA_HOME/webapps/data/data.dat or
>so. Is this possible or does anybody know a better way to place my
>data-files? And how do I connect to them?

Data files belonging to or referenced by your webapp should be under
your webapp's structure (or within your webapps .war file).  So if your
app is in the ROOT context, your data files should be in
$CATALINA_HOME/webapps/ROOT/data/data.dat.

Then, from your servlets, you could do:

InputStream is =
getServletContext().getResourceAsStream("/data/data.dat");
(and do whatever you need with the input stream, e.g. read it)
or
URL fileUrl = getServletContext().getResource("/data/data.dat");
To just get the URL for the file.  Don't assume this is a file:// or an
http:// URL.  It may be a jndi:/localhost/... type of URL as well.

You could also use 
String filePath = getServletContext().getRealPath("/data/data.dat");
approach, but that won't work if you're deploying out of a .war file.

Finally, if for some reason you cannot place your data files under your
own context, you can set a data file directory as a context parameter in
your web.xml.  For example:
<context-param>
  <param-name>dataFileDirectory</param-name>
  <param-value>/home/myname/mydatafiles</param-value>
</context-param>

Then in your servlets, you could do:
String dataFileDirectory =
getServletContext().getInitParameter("dataFileDirectory");

String filePath = dataFileDirectory + File.separator + "data.dat";
And go from there.

I hope you find this helpful,

Yoav Shapira
Millennium ChemInformatics

--
To unsubscribe, e-mail:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:tomcat-user-help@jakarta.apache.org>


Mime
View raw message