aspose file tools*
The moose likes Servlets and the fly likes Truly, deeply stupid question: how to access a file on server from servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Truly, deeply stupid question: how to access a file on server from servlet" Watch "Truly, deeply stupid question: how to access a file on server from servlet" New topic
Author

Truly, deeply stupid question: how to access a file on server from servlet

Chris Crawford
Ranch Hand

Joined: Jun 22, 2011
Posts: 87

I've been too embarrassed to post a request for help here, because I was sure that my problem is so elementary that it must be explained somewhere in kindergarten-level Servlets, but after three days of (discontinuous) searching the Web and two books, I still can't find an answer. Here's the problem: in the early stages of setting up the model code, I need to read an XML file. Try as I may, I cannot figure out how to get the servlet to read the file: everything I try returns the same FileNotFound Exception.

Programming context: Mac OS 10.7.5, Eclipse, using localhost

Here are some of the things I have tried:

Referencing the file by its name only:
Putting the file into WEB-INF
Putting the file into WebContent
Putting the file into the servlet directory

Referencing the file by its absolute position in the directory structure, as in "Drive/Users/me/Documents/workspace/projectName/WebContent/FileName"

Referencing the file by the ContextPath obtained by request.getContextPath()
Referencing the file by the FilePath obtained by request.getContextPath().getFilePath(). I wrote this to the console to make sure and got this filePath:

"/Users/me/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/projectName/"
to which I appended "WebContent/FileName".
Nope, it didn't work.

So, I know that I need to get a filePath relative to the servlet. Apparently request.getContextPath().getFilePath() doesn't work.

Any suggestions, as in "Did you remember to plug in the computer?"

Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2402
    
  28

If you need to read a file that resides on the same machine as where your webapp is running, you shouldn't use request.getCOntextPath. You will need the complete path to the file, and you will need to open it just like you were opening the file in an plain old Java program

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61424
    
  67

Is it part of the web app? If so, then getResourceAsStream() is the preferred approach.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Chris Crawford
Ranch Hand

Joined: Jun 22, 2011
Posts: 87

Jayesh, I tried opening the file with a hard file path -- actually, three variations on the hard file path as of now -- and none of them worked.

I'm beginning to suspect that I'm fighting Eclipse here. Could that be my problem?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61424
    
  67

Again, getResourceAsStream().

No hard file path needed.
Chris Crawford
Ranch Hand

Joined: Jun 22, 2011
Posts: 87

This is my third attempt at responding to your suggestions. The first two times, it didn't work, but while composing the statement of what didn't work, I came up with a possible question you might ask, and in checking it out, I got closer to the answer. Now I have something that sorta works. It's got a weird problem, but I'm not sure it's really the same as the original question. I'll continue poking at this thing and will either a) come back here to announce that I got it right; or b) post a new problem. But I won't waste your time until I've done my homework.

Chris
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

I don't know what you have done, but here's a suggestion: If your solution depends on the fact that you're using Eclipse (e.g. you mentioned some Eclipse directories in the path) then it's not the right solution. In real life you would only be using Eclipse to develop the servlet application, and then you would run it in a completely different environment which doesn't use Eclipse at all.
Chris Crawford
Ranch Hand

Joined: Jun 22, 2011
Posts: 87

Yes, I'm beginning to realize that, when it comes to servlet development, Eclipse is more of a hindrance than a help.
margaret gillon
Ranch Hand

Joined: Nov 12, 2008
Posts: 318
    
    6

Hi Chris,

In many posts people on the site with extensive servlet experience have talked about working outside an IDE. Bear mentioned it on your post last weekend. I have moved from Netbeans to Eclipse because Netbeans had problems compiling Bear's Frontman class. Now I am having problems with both of them making connections to an older MS-SQLserver because it needs an outdated jar to connect to the databases.

Here is the Tomcat page on organizing source code
Apache Application Developer's Guide -- Source Organization

There is an older post on the ranch talking about this very subject
How to run a servlet without netbeans or eclipse.

I would be interested in hearing from Bear, Paul, and others what enhancements they make to the directory structure and any other tools they use so they do not miss working in an IDE.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61424
    
  67

I do use an IDE; but only as a smart editor. I love the refactoring capabilities of IntelliJ, for example. But I do not run Tomcat in the IDE. To me, that way lies madness.

I structure the file system of my project so that I can point Tomcat at it and rn it live (so that changes to JSPs and static resources do not require a build). I use Ant to build outside of the IDE.

Other than that, nothing special.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

To tell the truth, I've downloaded the Java EE version of Eclipse and I'm using it. I had a plan to convert my Swing application to a web app, and probably some day I'll get around to that, but right now I'm not using it for web applications.

However the same thing applies to plain old Java applications which you develop in Eclipse -- use the classpath to find files which are associated with your application rather than hard-coding Eclipse directory names.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61424
    
  67

When using getResourceAsStream(), the path is relative the web context. No hard-coding eclipse or system file paths.
Chris Crawford
Ranch Hand

Joined: Jun 22, 2011
Posts: 87

For standalone applications, I had used System.getProperty("user.dir") to get the local directory for file access. It seemed to work just fine.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61424
    
  67

There's lots I have to say about that, but it's moot in this context.

Web apps are not standalone apps. For resources inside the web app, you should not be using any file paths that are not context-relative.
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 318
    
    2

Chris,

I'm having trouble understanding how this file is to be used. I have servlets that read files from a fixed location (config files), have files that are sent to the browser (icons, css, js), and files that are used as resources (templates).

They all use different ways to access them. BTW I use NetBeans and develop and test on Mac and Linux, deploy on Linux.

Perhaps if you tell us how you want to use this file we can be more clear on the methods available.

In short, for config files that I want the admin to edit, I put it in a "standard" location like /usr/local/<program name>/etc and open in the regular java way:

where configFile is a File object.

For stuff the browser gets I put the files in a Folder call Web Pages, same place the jsp's go (not in WEB-INF)

For resources they have their own folder called resources and i use the method Bear recommended.

Joe


It's not what your program can do, it's what your users do with the program.
Chris Crawford
Ranch Hand

Joined: Jun 22, 2011
Posts: 87

Joe, it's an XML file containing data used by the model, which is a complex simulation. I also have lots of image files that need to read at various times. As I mentioned earlier, it's trivial to access it in the standalone app using the standard File system and System.getProperty("user.dir"), but that obviously won't work in a servlet.
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 318
    
    2

Thanks Chris,

Let's talk first about the XML file.

When you say it's data for your simulation, do you mean that it is part of the application and is only changed by a programmer, or is it something the user uploads depending on what they want to simulate, or something in between where the user doesn't change it but a sysadmin with XML skills but not necessarily Java skills might change?

If it's really part of the program and only you or someone else maintaining the program changes, I suggest using a resource as it then becomes part of the war file.
If the user uploads it get puts in a temp directory and you deal with that way.
If it's something a sysadmin may change, then I suggest some sort of "config" directory in a fixed location. I use /usr/local/<myProgramName>/etc but any fully qualified path that is readable by Tomcat works.

For the image files, my question is can we consider them static content or do you manipulate them somehow in the servlet? IOW do you want a <a> tag to the image as is or do you have a url that calls some servlet to read modify and send them?

I tend to do both things. For the ones that are static they should go in the top level of the war file and are addressed as /<image file>.jpg in the <a> tag.

For the ones I manipulate or include in creating other images, I tend to put them in a mysql database but there's no reason they can't be a resource or in a fixed location (like the xml file).

One thing about resources that takes some getting used to is the path to use in the open statement. As Bear said "When using getResourceAsStream(), the path is relative the web context." What that means is the path is specified relative to the war contents. Here's a stack overflow thread: http://stackoverflow.com/questions/793213/getting-the-inputstream-from-a-classpath-resource-xml-file

As with most programming things there's a lot of ways to accomplish the same thing. I make no claim that my methods are optimal, but I can claim they work.

Joe
Chris Crawford
Ranch Hand

Joined: Jun 22, 2011
Posts: 87

Sorry, Joe, I should have specified up front that all these files are static. Read-only. I'm the only person who'll fiddle with them, and in truth they're already finished and are, as far as I'm concerned, cast in stone. I'll check out that StackOverflow thread. In my many searches for solutions, Google keeps giving me links to StackOverflow at the top of its list.

Thanks much.

Chris
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 318
    
    2

Hi Chris,

Since they are static, resources are probably best. The main advantage (as I see it) is that they are packaged as part of the program.

One trick that I use to figure out the path to use to open them is to use jar -tf and see where ant put them.

Good luck,

Joe
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Truly, deeply stupid question: how to access a file on server from servlet