0

I wanted to know how to create a file in the WebContent folder of a dynamic web project using Java?

The basic question remaining is how to get the path of the WebContent folder.

Note: No servlet is to be used!

Edit:

Okay, i am trying to create a new xml file from java method. I want the file to be created in the WebContent folder so that the file is created even when the application is deployed.

I am using Jboss, maven, JSF to create the dynamic web project. I need the xml file to pass data to highcharts. Please note that i will be using this method only.

Overview:

Create xml file on request

XML file to created in the WebContent folder itself

Use this xml file to pass data

yash
  • 189
  • 1
  • 4
  • 14
  • can you please more elobarate your problem how you are trying ? – Hemant Metalia Feb 09 '12 at 12:07
  • Not a good idea, especially since alot of newer app servers are using virtual filesystems to deploy your web content. – Perception Feb 09 '12 at 12:11
  • You cannot do this without violating the servlet spec, making your web application depend on vendor specific side effects. – Thorbjørn Ravn Andersen Feb 09 '12 at 12:12
  • There is no WebContent folder. WebContent is the name of the source of the webapp in Eclipse. When deployed on the server, there could be a folder, but there could also be a war file, which is not writable. And if you redeploy the app, you will lose all the created files. Store your files elsewhere. – JB Nizet Feb 09 '12 at 12:18
  • This is a matter of persisting data between invocations. Use a database or the Settings API for that. – Thorbjørn Ravn Andersen Feb 09 '12 at 12:21
  • @JBNizet so i cannot create any file dynamically in the webapp? – yash Feb 09 '12 at 12:21
  • You can, but not in the webapp's folder. You have your database, and the whole file system at your disposition. – JB Nizet Feb 09 '12 at 12:28
  • @JBNizet How will i access the file from client side using the $.get() method in highcharts? – yash Feb 09 '12 at 12:36
  • By having a servlet that reads the file from the file system, and writes it to the HTTP response output stream. – JB Nizet Feb 09 '12 at 12:45
  • @JBNizet servlet with JSF? please note that am not using JSP or anything else.. JSF facelets, JBoss AS 7, Maven, Oracle – yash Feb 09 '12 at 12:49
  • So what? A JEE webapp can define and use servlets even if it uses JSF. And I guess you could implement it using a JSF action (or whatever JSF calls them). Anything that can access an HttpServletResponse object and writes to its output stream is fine. – JB Nizet Feb 09 '12 at 12:52
  • @JBNizet thanks for your answer but i think it is going out of the real question. JEE and dynamic web, different.. i think either i am failing to make you understand or you are not understanding me completely. – yash Feb 09 '12 at 13:03
  • what is "dynamic web". This is not a standard Java term. Eclipse use the term "Dynamic web project" to refer to an Eclipse project to generate JEE web application. And you can define a servlet in a "Dynamic web project". JSF is a specification that is part of the global JEE specification. And JSF apps can only be deployed in a web container that supports JEE webapps. – JB Nizet Feb 09 '12 at 13:07
  • @JBNizet okay, can i get a working example? – yash Feb 09 '12 at 13:36
  • 2
    No. Use google. It's 15 lines of basic IO to read a file, and write what you read to a stream. I won't do the work for you. Google for Java IO tutorial. – JB Nizet Feb 09 '12 at 13:47

1 Answers1

-1

Glassfish solution. AbstractSearchPageBean - any of your class

private static final String WEB_INF = "WEB-INF";

public static String getWebPath() {
    final String webInfPath = getWebInfPath();
    return webInfPath.substring(0, webInfPath.indexOf(WEB_INF) - 1);
}

public static String getWebInfPath() {
    String filePath = "";

    java.net.URL url = AbstractSearchPageBean.class.getResource("AbstractSearchPageBean.class");
    if (url != null) {
        String className = url.getFile();
        filePath = (className.contains(WEB_INF)) ? className.substring(0, className.indexOf(WEB_INF) + WEB_INF.length()) : className;
    }
    return filePath.replace("%20", " ");
}

// Create file in webapp/xml directory
private void createXmlFile(String xml) {
    try {
        String fileName = System.currentTimeMillis() + ".xml";
        File file = new File(Settings.getWebPath() + File.separatorChar + "xml" + File.separatorChar + fileName);
        logger.debug("parseXML(): Creating file: " + file.getAbsolutePath());
        if (file.createNewFile()) {
            FileWriter fw = new FileWriter(file);
            fw.write(this.parseXML(xml));
            fw.flush();
            fw.close();
            logger.debug("parseXML(): file saved to the: " + Settings.getAPPLICATION_DOMAIN() + '/' + fileName);
        } else {
            logger.warn("parseXML(): Can't create file: " + file.getAbsolutePath());
        }
    } catch (IOException ioe) {
        logger.error("parseXML(): Bad save file: ", ioe);
    }
}
kornero
  • 1,109
  • 8
  • 11