tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "GB Developer" <gb107...@globallyboundless.com>
Subject RE: reading file in ServletContextListener
Date Mon, 28 Nov 2005 17:27:58 GMT
the javadoc for org.xml.sax.InputSource indicates it can take an
InputStream.

parser.parse(
   new InputSource(
      context.getResourceAsStream("/WEB-INF/classes/friends.xml")
   )
);


You might also get away with the following: 

parser.parse(
   context.getRealPath("/WEB-INF/friends.xml");
);

Note: the first uses getResourceAsStream, so the file must be found in a
classloader (thus, your file is under /WEB-INF/classes). The second is just
a real path to the file, so it can be anywhere.  The second is closest to
what you were trying initially. I'm not familiar with that API, so perhaps a
String that indicates "the location of the file" would work, but where "/"
is (without the getRealPath part) is "the directory from which Tomcat is
being started", and who knows (or should have to care) where that is.

In both examples above, 'context' would be obtained:
ServletContext context = servletContextEvent.getServletContext();




> -----Original Message-----
> From: Werner van Mook [mailto:werner@connecties.com] 
> Sent: Sunday, November 27, 2005 4:32 PM
> To: Tomcat Users List
> Subject: Re: reading file in ServletContextListener
> 
> 
> To make it more clear of what I try to do here is the 
> complete method  
> in which I try to read a file.
> 
>      private void list() throws Exception {
>          XMLReader parser = XMLReaderFactory.createXMLReader(
>                  "org.apache.xerces.parsers.SAXParser");
>          parser.setContentHandler(new PeopleHandler());
>          parser.parse("/WEB-INF/friends.xml");
>      }
> 
> This parse method want an InputSource. Sorry Wendy it didn't 
> work. I tried filling in a fully qualified URI. It completely 
> stops tomcat  
> from starting.
> The URI is to a file in the webapp which is trying to start.
> 
> And see there we have a chicken and egg problem.
> The web app needs the file to start.
> The file is not there if there is no web app.
> 
> 
> 
> 
> On Nov 27, 2005, at 11:06 PM, Wendy Smoak wrote:
> 
> > On 11/27/05, Werner van Mook <werner@connecties.com> wrote:
> >> In my class which implements ServletContextListener I try 
> to read a 
> >> file.
> >>   it looks like :
> >>     parser.parse("friends.xml");
> >>
> >> When I start tomcat 5.5.12 I get a FileNotFound Exception on the 
> >> friends.xml file.
> >>
> >> The file is in the root of my web-app.
> >> I also tried "/friends.xml" but alas it did not work.
> >
> > Try using ServletContext's getResourceAsStream() method to find the 
> > file, assuming that you have a 'parse' method that will take an 
> > InputStream.
> >
> > --
> > Wendy
> >
> > 
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> > For additional commands, e-mail: users-help@tomcat.apache.org
> >
> >
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message