Writing to a File from a Servlet in a Java EE Web Application

We will see how to write to a file in file system from a servlet. To write to a file in the root folder of a web app, we will need the real path to it. Using the path we can write to the file. Note that the WeContent folder you see in eclipse during development becomes the root folder of the web app, as we archive the contents of the WebContent folder to create a war file.  You can find more details on war creation and deployement @ packaging-and-deploying-a-web-application-into-standalone-apache-tomcat.

This note was prepared in response to a questen asked by a Java EE user through the facebook page. Original question from Java EE user is as follows: "i want to add new node to an existent xml file that is in the webcontent but changes are made in a temporary file <while running from eclipse> and when i restart tomcat all the changes are gone. So how to save this changes after deployment ?"

 

Step 1: Finding the real file path of root folder

First, we will look at three file path methods of the File class.

  • getAbsolutePath() - returns the absolute pathname string of this abstract pathname.

  • getCanonicalPath() - returns the canonical pathname string of this abstract pathname.

  • getPath() - converts this abstract pathname into a pathname string.

 

We can create a file instance within my Servlet as:

File file = new File(“test”);

 

What will getAbsolutePath(),getCanonicalPath() and getPath() print if I run the servlet from within eclipse?

  • getAbsolutePath() printed D:\eclipse-juno\test

  • getCanonicalPath() printed D:\eclipse-juno\test

  • getPath() printed test

Where D:\eclipse-juno is the folder where eclipse is installed

 

Now deploy the same project to a standalone tomcat and execute the same URL you see in eclipse browser, from a standard browser like chrome or firefox. This time you stop the server started from eclipse and start the tomcat server running startup.bat by going to the bin folder of tomcat installation.

What will getAbsolutePath(),getCanonicalPath() and getPath() print this time?

  • getAbsolutePath() printed D:\apache-tomcat-7.0.33\bin\test

  • getCanonicalPath() printed D:\apache-tomcat-7.0.33\bin\test

  • getPath() printed test

 

Now stop the server, go one directory back to D:\apache-tomcat-7.0.33 through command line and run startup.bat as bin\startup.bat. What will getAbsolutePath(),getCanonicalPath() and getPath() print if I run the servlet after starting tomcat from command line?

  • getAbsolutePath() printed D:\apache-tomcat-7.0.33\test

  • getCanonicalPath() printed D:\apache-tomcat-7.0.33\test

  • getPath() printed test

 

Note that the path changes from execution to execution based on from where the server was started.

What if I want to write to a file within the WebContent directory in eclipse which will be the project root folder (e.g. DynaServletProject)?

Now try ServletContext().getContextPath() and ServletContext().getRealPath:

  • getServletContext().getContextPath();

  • getServletContext().getRealPath("/");

  • getServletContext().getRealPath("/WEB-INF");

Below are the respective values when I ran the servlet within eclipse:

getServletContext().getContextPath() 
/DynaServletProject

getServletContext().getRealPath("/")
D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\DynaServletProject\

getServletContext().getRealPath("/WEB-INF")
D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\DynaServletProject\WEB-INF

 

Below are the respective values when I ran the servlet from standalone Apache Tomcat:

getServletContext().getContextPath()
/DynaServletProject

getServletContext().getRealPath("/")
D:\apache-tomcat-7.0.33\webapps\DynaServletProject\

getServletContext().getRealPath("/WEB-INF")
D:\apache-tomcat-7.0.33\webapps\DynaServletProject\WEB-INF

We can use getRealPath("/") to get the real path to the root folder.

 

Step 2: Writing to a file inside WebContent/Root folder from a servlet

Put the below code within the get method of servlet and run the servlet on server:

String contextPath = getServletContext().getRealPath("/");

String xmlFilePath=contextPath+"\\test";

System.out.println(xmlFilePath);

File myfile = new File(xmlFilePath);

myfile.createNewFile();

When run from within a standalone Apache Tomcat, this will create a file test in the root folder. Though the root folder is same as the WebContent folder, as you can see from above examples, the method getServletContext().getRealPath doesn’t point to the WebContent folder (root) when run from within eclipse. Instead it write to a temporary location. However, once you deploy your application to the Apache Tomcat servlet container everything will work as expected and file will be created in the root folder of the webapp.

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!