0

I'm doing a dynamic web project in java (servlet, jsp, css, html and json files) with eclipse using java. To run it I'm using Tomcat linked with eclipse.

The browser is able to get a file.json using this code:

`$.get("file.json", function( data ){...}

The file.json stays in WebContent:

WebContent/file.json

The dynamic web project has to save new information in the same file (WebContent/file.json).

For me it's not possible to use an absolute path, because I have to export it in .war and I have the necessity to give it (the project .war) to other people and it (.war) has to work in another operative system without a manual configuration. It is not possible for me to set eclipse and/or Tomcat because I will not have access to others' system.

I have tried to use a relative path but the new information has been saved in a temporary file of Tomcat.

I have also tried to use a servlet without a normal class in order to save the file.json but that did not work. The new information again gets written in a temporary file of tomcat.

In addition, sometimes the web application reloads the file. It cannot reload the temporary file but it must reload the WebContent/file.json.

I have also noted when I switch off tomcat, the file stays in a temporary folder and the file in WebContent does not get saved. This causes inconsistency of data.

My question is: in which way can I do this in order to write the file.json in this position (WebContent/file.json) only?

The code that I'm using:

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("doPost() from SaverServlet with parameter: "+request.getParameter("stringParameter"));
        ServletContext sc = request.getSession().getServletContext();

        String relativePath = "testJson.txt";
        String absolutePath = "/Use

rs/Dog/DynamicProject/WebContent/Database/Events.jsonEvents.json";
            String filePath = sc.getRealPath(relativePath);

            System.out.println("The path is: "+absolutePath);

    File file = new File(absolutePath);
    String content = request.getParameter("stringParameter");

    try (FileOutputStream fop = new FileOutputStream(file)) {
        System.out.println("tentativo di accedere al file");
        // if file doesn't exists, then create it
        if (!file.exists()) {
            System.out.println("il file non esiste, creazione del file...");
            file.createNewFile();
            System.out.println("File created");
        }

        System.out.println("Write on the file");

        // get the content in bytes
        byte[] contentInBytes = content.getBytes();

        fop.write(contentInBytes);
        fop.flush();
        fop.close();

        System.out.println("Done");

    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

But the code print using an absolute path:

Users/Dog/DynamicProject/WebContent/Database/Events.jsonEvents.json

This servlet has called by this JavaScript code:

function  saveEvents(events){
    $.ajax(
            {
                cache: false,
                url: 'SaverServlet',
                type: 'POST',
                data: 'stringParameter='+events,
                processData: false,
                //dataType: "json",
                success: function () {
                   // alert("ajax success");
                },
                error: function () {
                    debugger;
                    //alert("ajax failure");
                }

            })
snorlax
  • 157
  • 2
  • 3
  • 10
  • Please share the code you are using to write to this file. – Sampada Apr 07 '16 at 05:57
  • The alternative is just using a database. – BalusC Apr 07 '16 at 07:38
  • I have put the code that you have asked me. – snorlax Apr 07 '16 at 07:38
  • @BalusC Yes you are right but I cannot use a database because I cannot access to the other system. I have to create the project on my pc and after I have to pass my code only using a .war – snorlax Apr 07 '16 at 07:41
  • As you experienced yourself and as clearly explained in the duplicate, you **can and may not** save files in deploy folder. This makes no sense. Just save it outside or use a database. The "I cannot access to the other system" is a different problem which you need to solve together with the owner/admin of the other system. – BalusC Apr 07 '16 at 07:44

0 Answers0