optimus
optimus

Reputation: 729

Writing file in web Server without specifying real path

In my application, i got an function where i need to write a file inside webcontent folder, i got a folder called data and i need to write a new name, if i gave the real path( C:\Users\SanWin\workspace\RoleAccessControl\data\ActivityLog.txt) it writes the file but this has to change when the project is to run on different machine, so i used this to get the real path

ServletContext ctx = getServletContext();
        String path = ctx.getRealPath("/data/ActivityLog.txt");
        System.out.println(path);

when i print i got the following message in console

C:\Users\SanWin\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\RoleAccessControl\data\ActivityLog.txt

i dont know how the system gets this line

.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\

this is in eclipse, i was expecting something like C:\Users\SanWin\workspace\RoleAccessControl\data\ActivityLog.txt

how to get the real path i want to get rid of the lines (.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps), I looked and did all possible i seen in internet, but failed, sorry if I made an duplicate post,kindly excuse me Thanks for your time and responses

Upvotes: 1

Views: 1468

Answers (1)

user520458
user520458

Reputation:

In the Servers view in Eclipse you can double click your server to open a configuration screen which allows you to set a different deploy path (at least in Tomcat). Note that you can only modify that setting when there is nothing deployed, otherwise you will see the options grayed out.

enter image description here

The .metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ you are getting is because you have the Use workspace metadata option.

Anyway, if you deploy your war manually directly into Tomcat webapps folder, without Eclipse, you will be getting the correct path.

In other words, your code is correct and is giving you the correct path, you are just not liking the path where your app is being deployed.

Hope this helps.

Upvotes: 2

Related Questions