Link to home
Start Free TrialLog in
Avatar of mookie13
mookie13Flag for United States of America

asked on

Get Working directory in JSP

Hi all,

I have multiple JSP's and each one sits in a different directory with various images and such. My JSP outputs the contents of the directory along with some other information. Right now this only works if I hard code the directory path in the JSP.

I need to be able to have the path to the current working direcotry dynamically generated by the JSP. Is there a way to do this?

Avatar of rrz
rrz
Flag of United States of America image

Is this what you want ?  
<%
    String pathToJsp = application.getRealPath(request.getServletPath());
    File jspFile = new File(pathToJsp);
    String parentPath = jspFile.getParent();
%>
<%=parentPath%>
Avatar of mookie13

ASKER

Not exactly. This gives me the full path on the web server. In other words, it's returning this:

c:\WebSites\resin-3.0.18\websites\ROOT\folder1

I'm only looking for the directory relative to the site root. So I want it to return:

/folder1

or

/folder2 (depending on where this JSP is living.)

So I can build the link to the file:

/folder1/myImage.jpg

Right now it's doing this:

c:\WebSites\resin-3.0.18\websites\ROOT\folder1\myImage.jpg (which, of course, breaks.)

Thanks for your time in helping!!!
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tol_cv
Tol_cv

i think you can  use something like:

<%
String path = request.getContextPath();
%>
I do believe this works now!! Yay! Thank you!!
Your welcome, if you are talking to me.   rrz