Tomcat/Servlet Resource File Location [message #484398] |
Mon, 07 September 2009 07:56  |
Eclipse User |
|
|
|
Hi,
I've got a small problem, couldn't find anything about it:
I'm using TomCat (6.0.20) in Eclipse Europa with WTP. I want to write some small servlets with database access where I want the db information provided by properties-files.
My problem is the following: How can I deploy those resource files so I can find the properties-files in a relative manner? I have tried to include them in the src-directory, as external files, add them to the build path, ... But I don't know where I should put them or how I should publish the small web project to be able to relatively access the resource files. Can someone give me hints on what to do here?
cheers,
meta
|
|
|
|
|
Re: Tomcat/Servlet Resource File Location [message #484845 is a reply to message #484741] |
Wed, 09 September 2009 09:55   |
Eclipse User |
|
|
|
Andreas H. wrote:
> Larry Isaacs wrote on Tue, 08 September 2009 10:39
>> Andreas H. wrote:
>> > Hi,
>> > ...
>> > > cheers,
>> > meta
>>
>> You could try something like the following to get an InputStream to
>> read the properties:
>>
>> Servlet.getServletConfig().getServletContext().getResourceAs Stream(
>> "/WEB-INF/db.properties")
>>
>> Error and exception handling to be added.
>>
>> Cheers,
>> Larry
>
>
> Hello,
>
> That could be some sort of a icky solution. Sorry I didn't add the
> following information: I'm calling a backend object that's not a servlet
> and this object was meant to handle everything, including database
> connection which i would have needed the properties file for. I don't
> have a Servlet in there to get the configuration from. Of course I could
> get the Path in the servlet and try to pass it to the backend, but
> that's what makes it icky in my opinion. Could there be another way?
>
> Cheers,
> meta
Who is calling the backend object, and is it and/or the backend object
part of the web application?
The point is that ServletContext.getResource(),
ServletContext.getResourceAsStream(), Class.getResource() and
Class.getResourceAsStream() can all provide access to a resource within
a web application using a "path" that is guaranteed to work regardless
of deployment. Assuming the backend object is part of the web
application, a ServletContextListener could pass a ServletContext to the
backend object during web application initialization, or read the
properties and pass a Properties object.
Cheers,
Larry
|
|
|
|
Powered by
FUDForum. Page generated in 0.24461 seconds