?
Solved

Get Working directory in JSP

Posted on 2006-05-31
8
Medium Priority
?
677 Views
Last Modified: 2012-08-13
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?

0
Comment
Question by:mookie13
  • 3
  • 2
6 Comments
 
LVL 28

Expert Comment

by:rrz
ID: 16803644
Is this what you want ?  
<%
    String pathToJsp = application.getRealPath(request.getServletPath());
    File jspFile = new File(pathToJsp);
    String parentPath = jspFile.getParent();
%>
<%=parentPath%>
0
 

Author Comment

by:mookie13
ID: 16803884
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!!!
0
 
LVL 28

Accepted Solution

by:
rrz earned 500 total points
ID: 16804185
<%@ page import="java.io.File"%>
<%
  String servletPath = request.getServletPath();
  File jspFile = new File(servletPath);
  String workingFolder = servletPath.substring(0,servletPath.indexOf(jspFile.getName()) - 1);
%>
<%=workingFolder%>
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Expert Comment

by:Tol_cv
ID: 16807609
i think you can  use something like:

<%
String path = request.getContextPath();
%>
0
 

Author Comment

by:mookie13
ID: 16889061
I do believe this works now!! Yay! Thank you!!
0
 
LVL 28

Expert Comment

by:rrz
ID: 16889937
Your welcome, if you are talking to me.   rrz
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If anyone asked you to network diagram of the internet, it was drawn in the form of a fluffy cloud which further became known as cloud computing. Popularly cloud computing is defined as workloads that run over the internet in a commercial provider’s…
Social messanging services like WhatsApp and Facebook can help businesses in ways that many owners don't even imagine, giving new opportunities to connect with customers. Discover some of the most innovative things they can do for your company.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question