• Post Reply Bookmark Topic Watch Topic
  • New Topic

Servet initialisation settings from a file  RSS feed

 
David Garratt
Ranch Hand
Posts: 216
Eclipse IDE Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like my servlet to be able to read an xml file in which I want to hold config information include jdbc settings etc.

Will a servlet be able to to read a local file, and if so will there be any security settings that I need to accomodate on my Tomcat 5.5 server ?

Also if I open a file using a path "xml/myfile.xml" where in the webapps directory structure will the xml directory belong ?

Many thanks for any help

Dave
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Garratt:
Will a servlet be able to to read a local file...


If the file is within the hierarchy of the web app, then no problem (assuming you don't mess around with file permissions).

If it's outside, then the usual file system permissions need to be set accordingly, and any other normal security setting need to be correct. This is, of course, plarform-specific.

I'm assuming that the file will be within the confines of the web app.

Also if I open a file using a path "xml/myfile.xml" where in the webapps directory structure will the xml directory belong ?


That way lies misery and pain. You have no idea what the "current directory" will be, so any relative file references will be a crap shoot.

Rather, you should investigate the use of the ServletContext's getResourceAsStream().
[ March 10, 2007: Message edited by: Bear Bibeault ]
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, servlets can read files placed within their own web application (unless the server runs with a very unusual security manager). You could put the file into the WEB-INF/classes directory, and get an InputStream for reading it through

ServletContext.getResourceAsStream("myFileName.txt")
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One correction to Ulf's post. The ServletContext getResourceAsStream() method locates files relative to the context root, not the classes folder or classpath. (That's what the ClassLoader version of that method does).
 
David Garratt
Ranch Hand
Posts: 216
Eclipse IDE Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I wanted to call a routine using a library called dom4j which processes and xml file and takes a String containing a filename as a parameter can I get the filename using getResourceAsStream() ?

I've tried

String xmlfilename = getServletContext().getRealPath("xml/hosts.xml");

which seems to get the full path right from the root of my C: drive, but at the moment I'm getting and exception using this filename. Maybe it's because I'm appearing to access outsite of the webapps directory structure.

Not sure

Thanks

Dave
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DId you read the API docs for getRealPath()? It makes it quite evident that the path string you are passing is invalid.
[ March 10, 2007: Message edited by: Bear Bibeault ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!