This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
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.
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
posted
0
Code that shows error is new File(request.getContextPath()+"/docs/40Help.pdf");
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.
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.
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?
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.
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...
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 ]
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.
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.