When tomcat redeploy, the previous upload files are missing - java

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.

Related

Delete file from deployed web app from within the app

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/....

How to hot deploy newly created files to Apache Tomcat in Eclipse IDE?

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.

Eclipse Java Servlet access to higher level directory

I've installed Eclipse with web development and during download have installed Tomcat7 to dir E:\Eclipse\tomcat7.
When in Eclipse and trying to test the program I'm getting a realPath of
C:/Users/user/Documents/eclipseJEEWorkspace/javaWebPages/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/org.example.web/WEB-INF/classes/
as my real path and not what I expect which is E:\Eclipse\tomcat7\webapps\org.example.web/WEB-INF/classes/
1) how do I get the real path of the servlet rather than the temporary path...
or am I missing something with how Eclipse uses the installed server? If this is the way then do I have to continuously create the WAR and fiddle around recreating/deleting all the time?
The reason for this is I'm also trying to get a JAVA Servlet getting access to a file in a path higher than webapps. I'm new to servlets....
My development includes third party software that all reference an individual file our.properties so changing the structure is unfortunately not a option.
My directory structure is:
/tomcat
/mycompany
/properties
our.properties //the file we want to access
/*otherfiles
/html
/*not used in this context but to show usage
/javascript
/*not used in this context but to show usage
/webapps
/org.example01.web
/META-INF
/WEB-INF
/org.example02.web
/META-INF
/WEB-INF
/org.example03.web
/META-INF
/WEB-INF
How do I tell my Servlet to access the our.properties file?
I've tried getPath, getResource but without getting the first bit to work, I doubt I'll be getting anywhere fast.
Thanks
When you run Tomcat under Eclipse, via the Tomcat plugin, the webapps path is changed. Eclipse uses .metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ folder to deploy the project. That's not a temporary path, it's the actual path since your web application is deployed there.
If you run tomcat from command line, or as a service and deploy your web application, you'll see that the path of the servlet will be as you expected.
In order the change the default path to deploy for Eclipse, double click to your Tomcat under Servers view and modify Server locations section.

How can I create a file in my project folder through Servlet?

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?

new File not found in Webcontent. Need to refresh

Hello i have a java backend running on tomcat. java creates a .txt File that is saved locally in the Webcontent Folder. But the file is not found on the client side, altought it exists on the Webserver. After a refresh of my java workspace the file is found. Is there a way to tell tomcat to refresh / redeploy using java?
greetings!
Tomcat does not see your WebContents folder, but a copy create by the Tomcat server connector. Refreshing updates the copy.
What generates the .txt file? A separate program or code embedded in your web application?
There are a number of cases where the app would have to generate the file to be immediately available to the browser. For example, an image upload window. When the server creates the file from within WTP, it will have to also create the file at the published location where Tomcat is expected to pick it up. Open the server properties, tomcat arguments, catalina root parameter to find out where this location is. When the application is deployed this functionality will need to be disabled, of course.

Categories

Resources