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");
}
})