My objective is to create file in one of the folders of my Web-App such that when user clicks a download button, it's gonna download the file from that particular folder. For example, my file resides in "MyProjectName/WEB-INF/NewFolder/myfile.xls". Now, how do I create "myfile.xls" in that folder?
Here's what I have already tried:
I have set the file path to "/WEB-INF/NewFolder/" using this code in my Servlet:
String filePath = getServletContext().getRealPath("/WEB-INF/NewFolder/myfile.xls");
But the problem is, the value of "filePath" in the above code is:
C:\Users\MyUserName\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\MyProjetName\WEB-INF\NewFolder\myfile.xls
Hence "myfile.xls" is not getting created in "/MyProject/WEB-INF/NewFolder" and getting created in the metadata folder instead. Because of this, it's working only in my machine ie., localhost. When I am trying to access my application through another machine I am not able to download the file. I guess I am passing the absolute path to the JSP page which makes it impossible to download it from a different machine. So could anyone please let me know where I have gone wrong in doing this?
Your webapp is deployed to a web server on runtime and filePath points somewhere inside deployment folder not to a project one.
Anyway, why do you want to write into WEB-INF? If you for example redeploy application, then all files inside usually will be removed. Isn't better to use some folder inside server (for example in tomcat we can use ${catalina.home}/myfiles folder or some other outside web server?
Related
I have a Java web application that I build as WAR and deploy on Apache Tomcat. Let's say that I have a mechanism built-in my app to determine when it is successfully deployed and running on the server (I'll call it SuccessHandler). Conditionally in the SuccessHandler I need to programmatically delete a file from the project's deployment directory (say path-to-tomcat/webapps/my-project/file.txt) how can I do that?
Disclamer: I am able to load/delete files from the resources directories that reside in path-to-tomcat/webapps/my-project/WEB-INF/ after the deployment + I am able to delete the files using hard-coded absolute path but this is not what I am looking for. I want to be able to delete files no matter where the application is deployed.
You can't do it reliably. The inside of the application isn't writable in the general case. In particular, the archive may be executed in place and not exploded; even if it isn't, there's nothing keeping the server from restoring the file at every launch. You'll need another way to save your state.
It seems like you would need to delete the file only to write a new one in it's place.
Perhaps overwriting a file is a better approach?
I assume you have the directory where your application is deployed stored as a file, you could call directory.getAbsolutePath() on this and append your "path to file" to that path.
You can store and delete files outside of your webapp folder, for example in path-to-tomcat/lib/....
I have a peculiar problem. I am extracting the data from a database in the form of .csv file and then passing this file to d3.js for visualization.
The problem is, d3.js doesn't allow file:// based protocol (so I cannot give path to .csv file directly). The .csv file needs to be located on the server. But this file is generated on the run time.
I tried dumping the file in the same project folder, but as expected, the Eclipse doesn't take the updated file until next clean and build action.
Any idea how to work around this problem?
You need to place the file in the place where your application is deployed on Tomcat (e.g.: Tomact7\webapps\myProject...), not in Eclipse Workspace location, this way your js can access the File either by its URL or relative path.
I'm writing a website with the function file uploading.
And the Path I save the file is by ServletActionContext.getServletContext().getRealPath("xxxx")
It will save the file to webapps/mywebsit/xxxx
to allow the clients are able to access the data directly by http:// mywebsit/mebsite/xxxxx
the function is ok.
But when it comes to redeploy on tomcat here comes the problem.
When I redeploy by war file.
It seems sorts of remove the all webapps/mywebsit.
And deploy it with the new war file.
And the previous upload files are missing.
Is there anyway I can let tomcat don't remove my file while it's redeploying?
Or I should store files outside the webapps? But how can I let client be able to reach the file?
When tomcat redeploys your application, it will remove it's directory under /webapps/ completely and unpack the WAR from scratch. TI'd recommend keeping your files outside the webapp's directory. You could try using environment variables to point your application to directory you'd like to use, taking care that the user tomcat runs under has appropriate access rights.
I am coding a website using java servlets and am using eclipse and tomcat. When I test it using localhost, it works fine. But when I am deploying it on my actual website, the directory structure is messed up and the files are not called properly.
My eclipse directory structure on localhost is
Project Name
.src/packageName/java files
.WebContent/HTML files.
When I make a call from the html files, I use the relative location and tomcat automatically knows to look in the src/packageName folder. For example, from the /WebContent/login.html page makes a onClick call as follows,
. This will automatically trigger the java file in /src/packageName/welcome
When I am deploying it in my actual website, the WebContent/login.html is throwing an error WebContent/welcome file is not found. How do I tell my website to search in /src/packageName folder?
Hmm...have you been sure to package the application as a war for deployment.
I'm working on a project where I open a file, overwrite it, and save it as a new file. However, I'm having some difficulties with accessing the template file.
Right now, I believe my program is referencing the file using the path from my computer, however, if I were to export this program to a different computer, it probably couldn't find the file.
I have the file stored in a source folder for the project in eclipse. Is there a correct way to reference the file so that it will be able to be found on any computer?
I've attached an image on how my program is now referencing the file.
If the file is in the project folder, you don't need to specify a path at all. Simply using the filename "file.txt" can suffice where you would have normally put a path.
One alternative can be if you are using Tomcat server for deployment , then place this file in web-apps location of tomcat.
Accessing will be done using below code snippet
String filePath = System.getProperty("catalina.base") + "/web-apps";
This filePath will work on all computers because we set catalina base in all machines installing tomcat.