• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

How to set up a relative path

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

>
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic