Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 15th, 2003, 05:27 AM
operator smooth operator smooth is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 61 operator smooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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.

Reply With Quote
  #2  
Old March 15th, 2003, 05:29 AM
operator smooth operator smooth is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 61 operator smooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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...

Reply With Quote
  #3  
Old March 15th, 2003, 06:02 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
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.

Reply With Quote
  #4  
Old March 16th, 2003, 07:39 AM
operator smooth operator smooth is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 61 operator smooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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.

Reply With Quote
  #5  
Old March 16th, 2003, 06:32 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
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 06:38 PM.

Reply With Quote
  #6  
Old March 17th, 2003, 04:37 AM
operator smooth operator smooth is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 61 operator smooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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?

Reply With Quote
  #7  
Old March 17th, 2003, 04:49 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
Are you saying you benchmarked this and the overhead is 5ms? I would say that is not a lot to worry about.

Reply With Quote
  #8  
Old March 17th, 2003, 06:32 PM
operator smooth operator smooth is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 61 operator smooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Find path to file in same folder


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway