You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Morkeleb <as...@gmx.at> on 2004/11/05 17:28:25 UTC

How to use getServletConfig( ) outside a Servlet.

Hi.

I want to make a static class which access a configuration file in /WEB-INF/.
I tried <Classname>.class.getResourceAsStream("/WEB-INF/config-file.xml"); but it wont work.
So I thought I have to do it by using getServletConfig().getServletContext( ).getResourceAsStream("/WEB-INF/config-file.xml")
but I did not figure out how to do that because getServletCofig only works in Servlets, not in static beans.

Can you help me?

Thanx in Advance,
Lukas




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: How to use getServletConfig( ) outside a Servlet.

Posted by Radu Badita <ra...@gmail.com>.
Hi,

I had a problem similar to yours, and here is how I solved it:
1. made a Struts PlugIn (a class that implements
org.apache.struts.action.PlugIn) and has a method init(ActionServlet ,
ModuleConfig) and a String property (let's say configFileName). The
property will be automatically set by Struts at startup, and then the
init(...) method will be called.
2. in the init(...) method I read the file: 
InputStream is =
actionServlet.getServletContext().getResourceAsStream(configFileName);
3. the config file name is set from the struts-config.xml as follows:
    <plug-in className="mypackage.MyPlugIn">
        <set-property value="/WEB-INF/config.xml" property="configFileName"/>
    </plug-in>

This should work for you, too. Good luck! :-)


On Fri, 05 Nov 2004 17:28:25 +0100, Morkeleb <as...@gmx.at> wrote:
> Hi.
> 
> I want to make a static class which access a configuration file in /WEB-INF/.
> I tried <Classname>.class.getResourceAsStream("/WEB-INF/config-file.xml"); but it wont work.
> So I thought I have to do it by using getServletConfig().getServletContext( ).getResourceAsStream("/WEB-INF/config-file.xml")
> but I did not figure out how to do that because getServletCofig only works in Servlets, not in static beans.
> 
> Can you help me?
> 
> Thanx in Advance,
> Lukas
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 


-- 
Radu Badita
Galati, Romania
Phone : 004-0745-36.10.17
Email : radu.badita@gmail.com

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org