You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by "Stevenson, Chris (SSABSA)" <ch...@ssabsa.sa.gov.au> on 2001/05/30 08:44:27 UTC

[BUG T322] Bug in getServletContext().getResource() for relative URLs

Bugzilla is down so I am forced to send this to the list:

Relative URLs from a servlet resource do not work (Tomcat 3.2.2).

I have the following code in a Servlet:

URL propURL = getServletContext().getResource(
"/WEB-INF/connection.properties" )
URL configURL = new URL( url, "config.xml" );

The servlet throws a FileNotFoundException on configURL.getInputStream() 
some diagnostic info follows:

Could not load configuration file: file:persist.config.xml
propURL: file:F:\tomcat\tomcat-dev\webapps\ore\WEB-INF\connection.properties
configURL: persist.config.xml

Apache Tomcat/4.0-b5 works perfectly, but its using jndi.

Also, if I do the following:

URL configURL = new URL( url, "config.xml" );
configURL = new URL( configURL.toString() );

it works!

java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)

Full Exception is
java.io.FileNotFoundException: persist.config.xml (The system cannot find
the file specified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:64)
        at
sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:6
9)
        at
sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection
.java:133)
        at java.net.URL.openStream(URL.java:798)
        at ssabsa.control.Controller.<init>(Controller.java:52)
        at ssabsa.control.Controller.getInstance(Controller.java:74)
        at ssabsa.ore.WoreServlet.init(WoreServlet.java:47)
        at javax.servlet.GenericServlet.init(GenericServlet.java:258)
        at
org.apache.tomcat.core.ServletWrapper.doInit(ServletWrapper.java:317)
        at org.apache.tomcat.core.Handler.init(Handler.java:215)
        at
org.apache.tomcat.core.ServletWrapper.init(ServletWrapper.java:296)
        at
org.apache.tomcat.context.LoadOnStartupInterceptor.contextInit(LoadOnStartup
Interceptor.java:130)
        at
org.apache.tomcat.core.ContextManager.initContext(ContextManager.java:491)
        at
org.apache.tomcat.core.ContextManager.init(ContextManager.java:453)
        at org.apache.tomcat.startup.Tomcat.execute(Tomcat.java:195)
        at org.apache.tomcat.startup.Tomcat.main(Tomcat.java:235)




Re: [BUG T322] Bug in getServletContext().getResource() for relative URLs

Posted by DAK <da...@dotech.com>.
Yes, Have a look at this

Bug Id: 4447088
On the Bug Parade at the JDC. I submitted this after trying to use 
classloader.getResource() in a servlet. URL doesn't construct a proper 
URL string when the constructor you show is used. Works fine on 
unix/linux, but windows absolute paths can start with a letter, not a 
slash. I'd like to see tomcat use a different constructor so I can rip 
out some special code that checks the classloader first!

   David

Stevenson, Chris (SSABSA) wrote:

> Bugzilla is down so I am forced to send this to the list:
> 
> Relative URLs from a servlet resource do not work (Tomcat 3.2.2).
> 
> I have the following code in a Servlet:
> 
> URL propURL = getServletContext().getResource(
> "/WEB-INF/connection.properties" )
> URL configURL = new URL( url, "config.xml" );
> 
> The servlet throws a FileNotFoundException on configURL.getInputStream() 
> some diagnostic info follows:
> 
> Could not load configuration file: file:persist.config.xml
> propURL: file:F:\tomcat\tomcat-dev\webapps\ore\WEB-INF\connection.properties
> configURL: persist.config.xml
> 
> Apache Tomcat/4.0-b5 works perfectly, but its using jndi.
> 
> Also, if I do the following:
> 
> URL configURL = new URL( url, "config.xml" );
> configURL = new URL( configURL.toString() );
> 
> it works!
> 
> java version "1.3.1"
> Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
> Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)
> 
> Full Exception is
> java.io.FileNotFoundException: persist.config.xml (The system cannot find
> the file specified)
>         at java.io.FileInputStream.open(Native Method)
>         at java.io.FileInputStream.<init>(FileInputStream.java:64)
>         at
> sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:6
> 9)
>         at
> sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection
> .java:133)
>         at java.net.URL.openStream(URL.java:798)
>         at ssabsa.control.Controller.<init>(Controller.java:52)
>         at ssabsa.control.Controller.getInstance(Controller.java:74)
>         at ssabsa.ore.WoreServlet.init(WoreServlet.java:47)
>         at javax.servlet.GenericServlet.init(GenericServlet.java:258)
>         at
> org.apache.tomcat.core.ServletWrapper.doInit(ServletWrapper.java:317)
>         at org.apache.tomcat.core.Handler.init(Handler.java:215)
>         at
> org.apache.tomcat.core.ServletWrapper.init(ServletWrapper.java:296)
>         at
> org.apache.tomcat.context.LoadOnStartupInterceptor.contextInit(LoadOnStartup
> Interceptor.java:130)
>         at
> org.apache.tomcat.core.ContextManager.initContext(ContextManager.java:491)
>         at
> org.apache.tomcat.core.ContextManager.init(ContextManager.java:453)
>         at org.apache.tomcat.startup.Tomcat.execute(Tomcat.java:195)
>         at org.apache.tomcat.startup.Tomcat.main(Tomcat.java:235)
> 
> 
> 


RE: [BUG T322] Bug in getServletContext().getResource() for relative URLs

Posted by Marc Saegesser <ma...@apropos.com>.
According to the spec, the path passed to ServletContext.getResource() must
start with a "/".

> -----Original Message-----
> From: Stevenson, Chris (SSABSA) [mailto:chris@ssabsa.sa.gov.au]
> Sent: Wednesday, May 30, 2001 1:44 AM
> To: tomcat-dev@jakarta.apache.org
> Subject: [BUG T322] Bug in getServletContext().getResource() for
> relative URLs
>
>
>
> Bugzilla is down so I am forced to send this to the list:
>
> Relative URLs from a servlet resource do not work (Tomcat 3.2.2).
>
> I have the following code in a Servlet:
>
> URL propURL = getServletContext().getResource(
> "/WEB-INF/connection.properties" )
> URL configURL = new URL( url, "config.xml" );
>
> The servlet throws a FileNotFoundException on configURL.getInputStream()
> some diagnostic info follows:
>
> Could not load configuration file: file:persist.config.xml
> propURL:
> file:F:\tomcat\tomcat-dev\webapps\ore\WEB-INF\connection.properties
> configURL: persist.config.xml
>
> Apache Tomcat/4.0-b5 works perfectly, but its using jndi.
>
> Also, if I do the following:
>
> URL configURL = new URL( url, "config.xml" );
> configURL = new URL( configURL.toString() );
>
> it works!
>
> java version "1.3.1"
> Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
> Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)
>
> Full Exception is
> java.io.FileNotFoundException: persist.config.xml (The system cannot find
> the file specified)
>         at java.io.FileInputStream.open(Native Method)
>         at java.io.FileInputStream.<init>(FileInputStream.java:64)
>         at
> sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnect
> ion.java:6
> 9)
>         at
> sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURL
> Connection
> .java:133)
>         at java.net.URL.openStream(URL.java:798)
>         at ssabsa.control.Controller.<init>(Controller.java:52)
>         at ssabsa.control.Controller.getInstance(Controller.java:74)
>         at ssabsa.ore.WoreServlet.init(WoreServlet.java:47)
>         at javax.servlet.GenericServlet.init(GenericServlet.java:258)
>         at
> org.apache.tomcat.core.ServletWrapper.doInit(ServletWrapper.java:317)
>         at org.apache.tomcat.core.Handler.init(Handler.java:215)
>         at
> org.apache.tomcat.core.ServletWrapper.init(ServletWrapper.java:296)
>         at
> org.apache.tomcat.context.LoadOnStartupInterceptor.contextInit(Loa
> dOnStartup
> Interceptor.java:130)
>         at
> org.apache.tomcat.core.ContextManager.initContext(ContextManager.java:491)
>         at
> org.apache.tomcat.core.ContextManager.init(ContextManager.java:453)
>         at org.apache.tomcat.startup.Tomcat.execute(Tomcat.java:195)
>         at org.apache.tomcat.startup.Tomcat.main(Tomcat.java:235)
>
>