Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » ServerTools (WTP) » Tomcat/Servlet Resource File Location
Tomcat/Servlet Resource File Location [message #484398] Mon, 07 September 2009 11:56 Go to next message
Andreas H. is currently offline Andreas H.Friend
Messages: 21
Registered: August 2009
Junior Member
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 #484639 is a reply to message #484398] Tue, 08 September 2009 14:39 Go to previous messageGo to next message
Larry Isaacs is currently offline Larry IsaacsFriend
Messages: 1354
Registered: July 2009
Senior Member
Andreas H. wrote:
> 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

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
Re: Tomcat/Servlet Resource File Location [message #484741 is a reply to message #484639] Wed, 09 September 2009 05:49 Go to previous messageGo to next message
Andreas H. is currently offline Andreas H.Friend
Messages: 21
Registered: August 2009
Junior Member
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
Re: Tomcat/Servlet Resource File Location [message #484845 is a reply to message #484741] Wed, 09 September 2009 13:55 Go to previous messageGo to next message
Larry Isaacs is currently offline Larry IsaacsFriend
Messages: 1354
Registered: July 2009
Senior Member
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
Re: Tomcat/Servlet Resource File Location [message #484979 is a reply to message #484398] Thu, 10 September 2009 06:20 Go to previous message
Andreas H. is currently offline Andreas H.Friend
Messages: 21
Registered: August 2009
Junior Member
Thanks. I'll try it as soon as I can free up time, I was urged to do something different for now!
Previous Topic:Accessing Web Services with WTP
Next Topic:XML Editor
Goto Forum:
  


Current Time: Thu Apr 18 15:11:21 GMT 2024

Powered by FUDForum. Page generated in 0.02533 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top