• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to set up a relative path

 
Tom Shypulski
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.?
 
Tom Shypulski
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 65126
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Travis Hein
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic