#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    61
    Rep Power
    12

    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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    61
    Rep Power
    12
    Example:
    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...
  4. #3
  5. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    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:
    Code:
    pageContext.getServletContext().getRealPath("jspRelativePath/filesName");
    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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    61
    Rep Power
    12
    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:

    getRealPath("myfolder/myfile.txt");

    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.
  8. #5
  9. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    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:
    Code:
    String servletPath = request.getServletPath();
    String pathFromWebRoot = servletPath.substring(0, servletPath.lastIndexOf("/"));
    You could then affix this to the beginning of the file name you want to open.

    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 07:38 PM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    61
    Rep Power
    12
    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?
  12. #7
  13. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Are you saying you benchmarked this and the overhead is 5ms? I would say that is not a lot to worry about.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    61
    Rep Power
    12
    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.

    Rougly:

    Code:
    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

IMN logo majestic logo threadwatch logo seochat tools logo