You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Trevor Porter <tr...@woz.com> on 2003/01/03 21:33:56 UTC

Why a JNDI URL from getServletConfig().getResource() in Tomcat

I'm trying to read in a configuration file I have stored in my /WEB-INF 
directory from my servlet init() using 
getServletConfig().getResource(...) as follows:

public void init() throws ServletException
{
    ...

    String config = getServletConfig().getInitParameter("config");
    URL url = getServletContext().getResource(config);
    ...      
}

If I set the config init parameter to something like this (the 
/WEB-INF/test.xml file does exist):

    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/test.xml</param-value>
    </init-param>

I get back the following jndi URL from getResource():

   jndi:/localhost/sandbox/WEB-INF/test.xml

The same code running under resin returns a file URL as I would expect. 
The problem is that if I replace the getResource() call with a 
getResourceAsStream() under tomcat, the input stream will be empty.  Can 
anyone out there explain this behavior and how to work around it?


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Why a JNDI URL from getServletConfig().getResource() in Tomcat

Posted by Jacob Kjome <ho...@visi.com>.
Using getServletContext().getResourceAsStream("/WEB-INF/test.xml") should 
work just fine.  I don't see any reason why the inputstream would be 
empty.  I use this technique all the time.

Jake

At 12:33 PM 1/3/2003 -0800, you wrote:
>I'm trying to read in a configuration file I have stored in my /WEB-INF 
>directory from my servlet init() using getServletConfig().getResource(...) 
>as follows:
>
>public void init() throws ServletException
>{
>    ...
>
>    String config = getServletConfig().getInitParameter("config");
>    URL url = getServletContext().getResource(config);
>    ...
>}
>
>If I set the config init parameter to something like this (the 
>/WEB-INF/test.xml file does exist):
>
>    <init-param>
>      <param-name>config</param-name>
>      <param-value>/WEB-INF/test.xml</param-value>
>    </init-param>
>
>I get back the following jndi URL from getResource():
>
>   jndi:/localhost/sandbox/WEB-INF/test.xml
>
>The same code running under resin returns a file URL as I would expect. 
>The problem is that if I replace the getResource() call with a 
>getResourceAsStream() under tomcat, the input stream will be empty.  Can 
>anyone out there explain this behavior and how to work around it?
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>

Re: Why a JNDI URL from getServletConfig().getResource() in Tomcat

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Fri, 3 Jan 2003, Trevor Porter wrote:

> Date: Fri, 03 Jan 2003 12:33:56 -0800
> From: Trevor Porter <tr...@woz.com>
> Reply-To: Tomcat Users List <to...@jakarta.apache.org>
> To: tomcat-user@jakarta.apache.org
> Subject: Why a JNDI URL from getServletConfig().getResource() in Tomcat
>
> I'm trying to read in a configuration file I have stored in my /WEB-INF
> directory from my servlet init() using
> getServletConfig().getResource(...) as follows:
>
> public void init() throws ServletException
> {
>     ...
>
>     String config = getServletConfig().getInitParameter("config");
>     URL url = getServletContext().getResource(config);
>     ...
> }
>
> If I set the config init parameter to something like this (the
> /WEB-INF/test.xml file does exist):
>
>     <init-param>
>       <param-name>config</param-name>
>       <param-value>/WEB-INF/test.xml</param-value>
>     </init-param>
>
> I get back the following jndi URL from getResource():
>
>    jndi:/localhost/sandbox/WEB-INF/test.xml
>
> The same code running under resin returns a file URL as I would expect.

The format of the URL being returned is totally up to the container -- the
only requirement is that the container must be able to resolve this URL
and give you back an input stream from it.  (Besides, a file: URL would
not work if you ran the webapp directly from a WAR file :-).

> The problem is that if I replace the getResource() call with a
> getResourceAsStream() under tomcat, the input stream will be empty.  Can
> anyone out there explain this behavior and how to work around it?

As long as the "/WEB-INF/test.xml" resource exists in your webapp, my
experience has been that Tomcat always lets you read it.  Indeed, Struts
relies on this to read its own config file, and lots of Struts apps run
quite happily on Tomcat.

Could you create a small test case that illustrates the problem, and add
it as an attachment to a bug report in the bug tracking system?

  http://nagoya.apache.org/bugzilla/

Craig


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>