Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Having trouble reading a file

 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm attempting to implement a Java-based website using Servlets and JSP's. For this, I'm going through a hosting company which uses Tomcat 4. It should also be noted that this server is running Red Hat Linux (7.2, I believe).
I'm able to get a servlet up and running, but, for the application I'm attempting, I need to be able to open an XML file. This is where I seem to be stuck.
My current directory hierarchy looks something like this:

I have all of my classes arranged ina hierarchy (by package) under the WEB-INF/classes directory. I have my jsp(s) inside the jsp directory, and I have an xml file within the xml directory. However, I'm having trouble accessing that xml file.
Currently, I have the following code within my servlet:

This should set returnVal to true if the file is there and false otherwise. However, I continually get a AccessControlException.
My hunch is that, by putting the '/' prior to the xml directory, the application may be searching for the file at the root level, which is where the security issue comes in. How can I make it start looking at the level of the application context, which is set to /var/www/html (where the WEB-INF directory is located).
Anyone have any ideas about how I can accomplish this? It doesn't seem as if it should be that difficult to access a file, but I sure seem to be having problems with it.
Thanks,
Corey
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer to this problem is to mention the absolute path of the file you want to access, which in this case is, /var/www/html/xml/menuList.xml.
If you do not want to hardcode the path to the document root, you can use the following piece of code to get the document root.

You can do this in the init() method of the servlet so that you don't have to do this for every request.
The path to the filename would then be

Hope this answers your question.
-Bala.
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want people to be able to browse to your XML file, then this wouldn't work...

But.. if you can put it in the WEB-INF folder, then this post where David O'Meara responds, will show you a different way.

https://coderanch.com/t/354645/Servlets/java/Why-open-file

A more recent post on the same subject:
https://coderanch.com/t/354695/Servlets/java/getting-file-path-servlet

Seems David is in practice on answering this one.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic