user949550
user949550

Reputation: 21

Create and read from directories in tomcat

I need to create directories and read from them in a servlet.

If I wanted to create a folder in my webapps/appName directory, what should I do to achieve this?

Currently if I do:

File file = new File("conf\Conf.xml");

This will look into the directory "{TOMCAT_HOME}\bin\"

How do I point my default directory to "{TOMCAT_HOME}\webapps\appName\"

Upvotes: 0

Views: 3521

Answers (3)

BalusC
BalusC

Reputation: 1108632

If I wanted to create a folder in my webapps/appName directory, what should I do to achieve this?

Nothing. You should forget about this approach and look for an alternative approach. Whenever you redeploy the WAR or even whenever you restart the server, all changes made in the webapp folder structure will irreversibly get lost.

You need to prepare a fixed folder with read/write rights and set its absolute disk file system path as a configuration setting (properties/xml file) or as a VM argument. For example, /var/webapp/uploads. This way you can just use the File the usual way.

String root = getRootSomehow(); // Must return "/var/webapp/uploads".
File file = new File(root, "somefile.txt");
// ...

A completely different alternative is to use a database. This would be the only alternative if you are deploying your web application to a host which does not allow you to create folders outside the webapp context.

Upvotes: 1

Prasanna Talakanti
Prasanna Talakanti

Reputation: 2394

You just need to get real path from servlet context that would give the path you are looking for , do some thing like this, this code will create directory with current date as name. If you want avoid delay in servlet start up create a thread and delegate the directory creation to the thread. You can save the directory path to servlet context like this.

private static final String DIR_SERVLET_CTX_ATTRIB = "directoryPathAttrib";
public void init() throws ServletException {
    StringBuilder filePathBuilder = new StringBuilder(getServletContext().getRealPath("/").toString());
    filePathBuilder.append(File.separator);
    filePathBuilder.append(new SimpleDateFormat("MM-dd-yyyy").format(new Date()));
    System.out.println("Directory path: "+ filePathBuilder.toString());
      File file = new File(filePathBuilder.toString());
      if(!file.exists())
      {
          file.mkdir();
          System.out.println("finished creating direcotry");
      }
     getServletContext().setAttribute(DIR_SERVLET_CTX_ATTRIB,  filePathBuilder.toString());
}

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, java.io.IOException 
{
    String dirPath = (String)getServletContext().getAttribute(DIR_SERVLET_CTX_ATTRIB);
    File file = new File(dirPath + File.separator + "test.txt");
    FileOutputStream fis = new FileOutputStream(file);
    fis.write("Hello".getBytes());
    fis.flush();
}

Upvotes: 0

Mac
Mac

Reputation: 1642

Questions of why you'd do this aside.... The most portable way would be to add an <init-param> to your servlet and pass the path in that way.

You could also use System.getenv() to get the TOMCAT_HOME environment variable, assuming it has been set.

Upvotes: 0

Related Questions