This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Determining the location of the file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Determining the location of the file" Watch "Determining the location of the file" New topic
Author

Determining the location of the file

Roger Fed
Ranch Hand

Joined: Oct 17, 2010
Posts: 82

Hello,
In developing my web application i have used a file to register and search data
the is stored beside the class that do the operations of reading and writing
well, when i write the name of the file as a path for it as follow

the compiler flag FileNotFoundException why???
thanks...


I Hear I Forget, I See I Remember, I Do I Understand.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Web apps have no concept of a "default directory", so you can't use relative paths for files. You either need to use absolute paths, or -for directories inside your web app directory- use ServletContext.getRealPath(...).

Oh, now I see it's about the compiler error - "new File(...)" throws that exception, so you need to handle it properly (using a try/catch block).
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60738
    
  65

getRealPath() won't work in all circumstances. It's best to use ServletContext.getResourceAsStream().


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

new File(...) will never throw an exception.
Roger Fed
Ranch Hand

Joined: Oct 17, 2010
Posts: 82

this isn't servlet
it is normal class so the ServletContext will not work here
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60738
    
  65

Get the stream at the servlet level and pass it to the normal class.

There's no way that your "normal" class can determine the location of the file (unless the full path is hard-coded -- not a good idea) without the help of the servlet layer.

Or, place the file somewhere in the classpath and use ClassLoader.getResourceAsStream().

Or, store the absolute path of the file in a properties file that you read at run time.
Roger Fed
Ranch Hand

Joined: Oct 17, 2010
Posts: 82

this is the class
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60738
    
  65

If it's a normal class why is it importing servlet classes?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60738
    
  65

And, what's the purpose of reading the file in the first place?
Roger Fed
Ranch Hand

Joined: Oct 17, 2010
Posts: 82

i was trying to use ServletContext
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60738
    
  65

Depending on what you are really trying to do the best solution is likely to not be moving servlet information into the class.

But you haven't answered my latest question yet.
Roger Fed
Ranch Hand

Joined: Oct 17, 2010
Posts: 82

this class will search in the file about the username and password specified
and if found it return a User object that will be put in the session later
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60738
    
  65

Roger Fed wrote:this class will search in the file about the username and password specified
and if found it return a User object that will be put in the session later

What's in the file that needs to be looked up? Is this not something better handled by a database?
Roger Fed
Ranch Hand

Joined: Oct 17, 2010
Posts: 82

Yes, but i still doesn't have the skills to deal with database
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60738
    
  65

In my opinion you are spending a lot of time trying to do things the wrong way, and that time would be better spent learning the skills you lack to do it the right away.
Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

If you're just using this for a learning experience (your reasoning for not wanting to involve a database), you may want to consider making the username / password either servlet init parameters, or context parameters for the web application. DO NOT DO THIS WITH A SECURE PASSWORD, but if you want to use it just for testing, this might be a good approach.

Bear's right. For an actual enterprise solution, a database (or LDAP server) is a better option.





OCPJP
In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
 
Don't get me started about those stupid light bulbs.
 
subject: Determining the location of the file
 
Similar Threads
File attachement
Ant error: Make sure you have it in you classpath
The IE and Netscape conflict again!! :-)
Should I must program for closing data file?
a file in your Jar, How do I access it in code?