wood burning stoves 2.0*
The moose likes Servlets and the fly likes Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet" Watch "Servlet" New topic
Author

Servlet

Sachin KumarSri
Greenhorn

Joined: Jun 12, 2008
Posts: 9
Hi,
I need to open a pdf file using servlet ie. i have link on my jsp that calls servlet and in that servlet i write the code to open the file using response.
problem is i want to refer pdf file from servlet using contextroot but it is not working but when i am using getRealPath Method it is working.
can any body tell me the resion.
code is File f=new File((request.getRealPath("\\")+"\\doc\\ABHelp.pdf"));

SGR is the name of War file and under that doc is the folder that contains the file.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Define 'not working'

Also, please try to UseAMeaningfulSubjectLine
Sachin KumarSri
Greenhorn

Joined: Jun 12, 2008
Posts: 9
"Not Working" means it is giving error that file is not found.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4168
    
  21

Originally posted by Sachin KumarSri:
Hi,
I need to open a pdf file using servlet ie. i have link on my jsp that calls servlet and in that servlet i write the code to open the file using response.
problem is i want to refer pdf file from servlet using contextroot but it is not working but when i am using getRealPath Method it is working.
can any body tell me the resion.
code is File f=new File((request.getRealPath("\\")+"\\doc\\ABHelp.pdf"));

SGR is the name of War file and under that doc is the folder that contains the file.


You showed us the working code. Now show us the code which generates the file not found exception.


Steve
Sachin KumarSri
Greenhorn

Joined: Jun 12, 2008
Posts: 9
Code that shows error is
new File(request.getContextPath()+"/docs/40Help.pdf");
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

One thing to understand about getRealPath is that is only works if your web app has been deployed from directory on the file system. If your app was deployed from an unpacked war file getRealPath will return null.

A more robust way to access files from within your webapp is to use getResourceAsStream. If your files are located outside the app's file structure, you're better off reading the file with java.io.FileReader, using a configurable property that let's the application know where to find the file.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Sachin KumarSri
Greenhorn

Joined: Jun 12, 2008
Posts: 9
I checked with both the way getResourceAsStream is working fine but when i used getRealPath every thing was fine ,it is showing open/save dioglog
box but this time it is asking for password,while file is not password proctected.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Please be sure to take the time to compose descriptive subjects for your posts; read this for more information.

Using a title of "Servlet" in a forum completely dedicated to questions on servlets isn't very helpful. What if everybody named their post "Servlet"?

You can go back and change your post to add a more meaningful subject by clicking the .
[ July 29, 2008: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4168
    
  21

Originally posted by Sachin KumarSri:
Code that shows error is
new File(request.getContextPath()+"/docs/40Help.pdf");


This would try to access the file relatively from whatever the 'current working directory' is, which often is the directory where you server's executable is (but this can't be counted on).

You need a real reference to the file location on the operating system, and that is what the getRealPath does. As was mentioned, getRealPath only works when the WAR is exploded for operation, which is NORMAL, so unless you specifically change things, the getRealPath should be okay.

The getResourceAsStream is a much more robust way of getting the data, so in general you should be using that (as has been stated) to avoid an error from occurring when the environment is out of your control.

You say you get a password prompt now. What code are you using 'now' that causes the password prompt?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Originally posted by Steve Luke:
This would try to access the file relatively from whatever the 'current working directory' is ...
More correctly, it would be relative to the document base (web app root) of the context. There is no defined concept of "current working directory" for Java web apps.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4168
    
  21

Originally posted by Bear Bibeault:
More correctly, it would be relative to the document base (web app root) of the context. There is no defined concept of "current working directory" for Java web apps.


No, that is incorrect. It would get a File relative to the path the processes running the server is using as its active directory. For *Nix systems this is usually someplace in the user folders for the user running the server(but for *nix I think the path would be considered an absolute path), while on Windows machines it is usually the directory where the executable exists.

While the Servlet Context doesn't have a concept of a current working directory, the File() constructor uses the OS to generate the real path from the relative path provided. As per the API for java.io.File:

A relative pathname, in contrast, must be interpreted in terms of information taken from some other pathname. By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.

Saying the File is opened relative to the application base would suggest his code:

would work by providing the real path:
<Application Path>/<Context Name>/docs/40Help.pdf.
But it doesn't. If for instance, he is using Tomcat on Windows and his startup script didn't modify the working directory, then it would look for the file:
C:\Program Files\Apache Software Foundation\Tomcat 6\bin\docs\40Help.pdf

The only reason I am dwelling on this is because I think it specifically applies to the problem the user is having...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Originally posted by Steve Luke:
No, that is incorrect

Actually, we're both wrong!

Based upon earlier conversation, my mind had the path wrapped by getRealPath(), and that's not what the OP's code does. My bad.

However, because the OP's code is directly using the path as a file system path, it will always begin with / and the non-deterministic current directory will not come into play.
[ July 29, 2008: Message edited by: Bear Bibeault ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

In my experience, it is deterministic, just confusing and not very useful
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

I say non-deterministic because it is dependent upon which container is being used and even how that container is configured on the file system. Obviously not something to be depended upon within the code of a web application.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4168
    
  21

Originally posted by Bear Bibeault:

Actually, we're both wrong!

...

I say non-deterministic because it is dependent upon which container is being used and even how that container is configured on the file system. Obviously not something to be depended upon within the code of a web application.


Agreed. The moral of the story: "Don't do that."
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Originally posted by Steve Luke:
Agreed. The moral of the story: "Don't do that."

I do love a story with a happy ending!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Servlet
 
Similar Threads
IFrame and word document
Doc to Pdf conversion using Java Code
Download Component functionality using servlets
Adobe Acrobat gives error to open programmatically generated pdf attachment.
Title: How to open new browser from liferay portal using struts ?