File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes How to set up a relative path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How to set up a relative path" Watch "How to set up a relative path" New topic

How to set up a relative path

Tom Shypulski

Joined: Jan 02, 2009
Posts: 8
I'm putting together a website with JSP pages and servlets. I have built a admin control page for some site management. I would like the ability to add a directory like "http:\\localhost\directory\path\files" by typing the path "\directory\path\files" into a text field on a jsp page, store path in MySQL db for use in creating links. I want the path to append to the end of http:\\localhost\. Is this possible using a relitive path as opposed to absolute path. It seems that when I create the new directory File f1 = new File() in my servlet it is relative to my WEB-INF folder, I want it to be reative to my ROOT directory.
Travis Hein
Ranch Hand

Joined: Jun 06, 2006
Posts: 161

will return a file object to the location of the top of your web application on the file system, for example, "c:\program files\apache tomcat 6\webapps\myapp"

From the file object, you can get work to parse out or extract the prefix or pieces of it, and do a relative path from it.?

Error: Keyboard not attached. Press F1 to continue.
Tom Shypulski

Joined: Jan 02, 2009
Posts: 8
Thanks Travis, That did give me what I'm looking for but I have another problem.
Can I create folders in this manner in the Tomcat ROOT Directory. My folders fail to create in the ROOT dir when obtianing the path from the solution provided. I am able to create folders one level up in the webapps directory. Are there settings I need to configure without leaveing web server unsecure.

Is it best to keep my .jsp's under the ROOT directory?

Here are snipets of code I'm using to create the directories.

From .jsp I get the ROOT path then append the folder\ folders I want to create by passing a form value.

From servlet I call doCreateDir(relativeDir);

Here is code to create path. Returned is Failure to create directory.

Thanks again.

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63852

Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.

[Asking smart questions] [About Bear] [Books by Bear]
Travis Hein
Ranch Hand

Joined: Jun 06, 2006
Posts: 161
if one or more folders within the path do not exist we need to first create them

for example,
"/web/app/root/" + "/folder1/file.jsp"

we know /web/app/root/ exists, but if /folder1/ doesn't exist inside it, we won't be able to write to file.jsp , and the same in the more general case of many folders in the relative path component.

so, try adding a call to mkdirs() of the File object,

I agree. Here's the link:
subject: How to set up a relative path
It's not a secret anymore!