2

I have a properties file in resource folder under my project. I am able to load the file, but when I try to write into it, it does not get updated. I am sure this question was asked several times. I will be very thankful if someone provides me a solution.

Servlet

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    String name = request.getParameter("appName");
    String link = request.getParameter("appLink");
    String database = request.getParameter("appDB");
    String webServices = request.getParameter("appWebService");
    System.out.println(name);

    Properties prop = new Properties();
    String propFileName = "server_url.properties";

    InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(propFileName);
    prop.load(inputStream);
    System.out.println(propFileName);
    if (name.equalsIgnoreCase(prop.getProperty("DemoApps_name"))) {
        prop.setProperty("DemoApps_DataBase", database);
        prop.setProperty("DemoApps_Links", link);
        prop.setProperty("DemoApps_WebServices", webServices);
        System.out.println(prop.getProperty("DemoApps_DataBase"));
    }
    prop.store(new FileWriter(propFileName),"Update App details" );     System.out.println(propFileName +" "+"written successfully");


    response.sendRedirect("updateappstatus.jsp");

}
V02169194
  • 67
  • 1
  • 2
  • 15
  • 1
    `new FileWriter(propFileName)` are you sure! you are writing the same file location!!! as `propFileName ="server_url.properties"` – Subhrajyoti Majumder Mar 09 '15 at 09:13
  • No.. when I tried to get the absolute path, it is showing a different path -`C:\Desktop\eclipse\server_url.properties` – V02169194 Mar 09 '15 at 09:24
  • If you don't specify an absolute path, the relative path depends on how you start the application. if you start it from Eclipse, classpath-resources are loaded from the configured bin folder (which is overwritten on changes). – Andy Mar 09 '15 at 09:35
  • check this http://stackoverflow.com/questions/676250/different-ways-of-loading-a-file-as-an-inputstream – Naren Mar 09 '15 at 09:35
  • I don't want to specify the absolute path as it wont work when deployed in production server. – V02169194 Mar 09 '15 at 09:36
  • @Naren I already checked. It is given about loading properties file. I want to update the same property file. – V02169194 Mar 09 '15 at 09:39

2 Answers2

1

First, you are not writing to the same location as the original file. You read the original file from the class path, while you write to the current working directory.

Second, you should not be able to write into your application's class path. If you ship a property file inside your web app, you cannot modify this (I don't know the Servlet spec to that detail but either there is a contract forbidding you to do so, or it could be even possible for container implementors to keep your web app's classes inside the packaged jar/war file, so the classes and resources are read only.

What you could do is to ship a default properties file which you read as fallback, and keep your writable properties file in a location outside the container as described in the answers to this question: Where/how to store persistent data with tomcat?

Community
  • 1
  • 1
Stefan Winkler
  • 3,871
  • 1
  • 18
  • 35
1

I had the same problem. The web project will have 2 different running location. When you run it on eclipse, the project will be loaded to Eclipse workspace directory. So if you want to access your property files from that location you can do the following.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   
    String currentPath = getServletContext().getRealPath("/") + "/WEB-INF/classes/com/server_url.properties";
    String name = request.getParameter("name");
    System.out.println(name);
    try {
        System.out.println(getPropertyValue("name",currentPath));
        setPropertyValue("name", name,currentPath);
        System.out.println(getPropertyValue("name",currentPath));
    } catch (Exception e) {
        e.printStackTrace();
    }
}


public static void setPropertyValue(String key, String value,String path) throws Exception {
    java.io.InputStream inputStream = null;
    FileOutputStream outputProperty = null;
    try {
        Properties projectProperties = new Properties();
        inputStream = new FileInputStream( path);
        projectProperties.load(inputStream);
        outputProperty = new FileOutputStream( path);
        projectProperties.setProperty(key, value);
        projectProperties.store(outputProperty, null);
    } catch (Exception e) {
        throw e;
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
        if (outputProperty != null) {
            outputProperty.close();
        }
    }
}

public static String getPropertyValue(String key,String path) throws Exception {
    java.io.InputStream inputStream = null;
    try {
        Properties projectProperties = new Properties();
        inputStream = new FileInputStream(path);
        projectProperties.load(inputStream);
        return projectProperties.getProperty(key);
    } catch (Exception e) {
        throw e;
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
    }
}

These codes will be used to get or set property values. Another best way is, having property files outside the web application.

Pasupathi Rajamanickam
  • 1,982
  • 1
  • 24
  • 48