March 15th, 2003, 05:27 AM
Find path to file in same folder
I want to get absolute(or relative to "document root") path to a file, from a path relative to current JSP page or servlet.
In ASP/ASP.NET i would do:
string absolute_filename = Server.MapPath(relative_filename);
Please, tell me how to accomplish that in JSP or Servlet.
March 15th, 2003, 05:29 AM
My file, test.txt is in the same directory as my JSP page.
I have no idea in which folder my JSP script resides.
I want to read content of test.txt.
That's about all...
March 15th, 2003, 06:02 PM
I am not where I can test this, but I know that if you are accessing web files relative to jsp using an include or whatever, you can merely use the files document-relative path to the jsp. If it is in the same directory just use the file's name.
However, you may be wanting to open this using a RandomAccessFile if any of your other posts mean anything. In that case you probably tried a document-relative path. Off the top of my head I thought that would work. But if it isn't, then you can get the "real" o/s path of a file from a jsp using:
You give it a web relative path and this will return a path relative to the root of the o/s file system. You will be able to open the file using a File object this way.
March 16th, 2003, 07:39 AM
getRealPath() can only translate a path, as if it was relative to the "document root", so if I have a folder "myfolder", which contains both the JSP and the text file, I need to read, then I can't use getRealPath() for much.
Instead, I would have to know the name of my folder and where it is relatively to the web-root. For example:
But I want some way to access files using a path relative to current folder, not relative to the web-root. For this, getRealPath() doesn't work.
March 16th, 2003, 06:32 PM
Yes, without being able to test it I was mistaken. The getRealPath method prefixes the file system path *up to* the web root. You are responsible of supplying the path from the web root on.
I know of no method that will give you the path up to the jsp without also returning the jsp's name also. Nor is there a method that returns only the jsp name (which could be used to remove it from the request.getServletPath() method).
You could maybe use something like this:
You could then affix this to the beginning of the file name you want to open.
String servletPath = request.getServletPath();
String pathFromWebRoot = servletPath.substring(0, servletPath.lastIndexOf("/"));
Apparently Sun expects you will always know where in the web app the current jsp is located, which means you would know the current path from web root. I cannot say I have ever run into a problem where I made a jsp where I did not know where it was located in the application, but I can see where it might happen.
Unless you are just trying to make the jsp as maintanance free as possible so if it ever gets moved you won't have to change the code. In which case you might have to live with this problem.
if you find a something that works I would be interested in knowing. Please post here.
Some path info stuff: http://jguru.com/faq/view.jsp?EID=148
Last edited by Nemi; March 16th, 2003 at 06:38 PM.
March 17th, 2003, 04:37 AM
Well, Sun has not thought this one through...
I need this for some samples, I am making, that show how to use my template processing "component". So I just expect the users to unzip the samples folder somewhere, and it should work.
I probably use your solution for this, as this is good enough for a demo. In production envirement however, I think that creating a path this way on every request would introduce some overhead.
By the way: increasing processing time by 5 ms at each request, is it much?
March 17th, 2003, 04:49 PM
Are you saying you benchmarked this and the overhead is 5ms? I would say that is not a lot to worry about.
March 17th, 2003, 06:32 PM
No, actually, I "benchmarked" my template engine, to be more specific, generating something like 100 rows x 7 columns table using a template file.
It was only simple timing, much the same way I used to time file reading operations, we discussed.
long start = ...
Template tpl = new Template(filename);
select template table row;
for (int i=1; i<=100; i++)
set field 1 in template row to some value
set field 7 in template row...
deselect template row
long end = ...
set a field to output end - start