sosegon12
sosegon12

Reputation: 61

File path gwt app hosted mode

I'd like to know how to get the path of a file to access to it in hosted mode. My app is reading some files using a servlet. Originally the files are located under the folder "war" (e.g. "war/data/file1.txt"), in development everything works well, the servlet reads the file and sends the data to the client. However, I'm having issues when I deploy the app in Tomcat.

I copied the content of the folder war, and put it in a folder under the "webapps" directory (in Tomcat). In development mode, I access to the data file using the path "data/file1.txt" but this doesn't work in hosted mode because I found out that when using that path, the servlet looks for the file in the "bin" folder of Tomcat installation directory, not in the folder of the application.

So, I'd like to know how to programatically find the correct path to access the file and avoid problems when deploying the application to Tomcat or any other server.

Upvotes: 1

Views: 1112

Answers (2)

pistolPanties
pistolPanties

Reputation: 1890

Take a look at GWT.isProdMode(). It returns true if running in production mode, and false if in development mode. You can choose the apropriate path by placing it in an if statement

Upvotes: 0

okrasz
okrasz

Reputation: 3974

Use ServletContext.getRealPath(). You can get instance of ServletContext from Servlet.getServletConfig().getServletContext().

The reason why you saw it working in development mode, but not in Tomcat is that the path normally is relative to working directory. Probably you could also make Tomcat work if you started it when being in your web application directory or modifying Tomcat shortcut to have working directory in you web app. But you definitely shouldn't rely on it.

Upvotes: 1

Related Questions