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/....
Related
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.
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?
Instead of using a certain jar in my WEB-INF/lib folder, I want to use its source code (same directory structure and everything) in my WEB-INF/classes folder, so that I may be able to modify its classes more story.
Yet (re)starting my tomcat after deleting the original jar and uploading the corresponding directory into WEB-INF/classes gives me the following error:
SEVERE: Error configuring application listener of class no.something.something1.http.LifecycleListener
java.lang.ClassNotFoundException: no.something.something1.http.LifecycleListener
I am certain that the directory path is the same as the one inside the jar. Also, I have previously tried using classes in my WEB-INF folder for this web application, and tomcat has also been unable to load them, for some reason.
Does anyone know how I go about troubleshooting this error?
Tomcat can only load .class files, it doesn't know what to do with raw source code files. Tomcat doesn't do hot loading of .class files like that anyway. You would have to restart the application or server after you recompiled them either way, packaging them as a .war isn't that much of a burden either way once you automate it.
If you take the time to automate the build and deployment of a proper .war you can just rebuild the .war and it will automagically undeploy and redeploy the application itself, which is many times faster than restarting the entire server.
You can't do what you are trying to do the way you are trying to do it. Tools like JRebel address these issues, but I don't find them as useful as their marketing makes them sound.
You could use embedded tomcat (or embedded jetty) to map directory structure of the application as you like. It probably will require some tinkering if you need some JNDI resources within your application but still worth the trouble.
Here's an example
I have installed and configured tomcat+solr on my personal linux machine and windows as well. I was able to get them working fine. I'm very new to Java and how the file structure works. (i.e. knowing where to put war files and what WEB-INF is) So now that I am ready to install solr and configure it on my clients shared hosting plan, the directions are different from what I did before. I dont want to mess this up and apparently the webserver reboots daily and I dont think I can do it manually which means I have one shot at this every day.
Here is the directions for installing a tomcat servlet on his hosting provider:
http://www.apluskb.com/scripts/Where_do_I_put_my_answer1186.html
As you can see I need to install solr under the html/WEB-INF directory, but read what it says.. its very confusing:
"All Servlets should be uploaded in the /html/WEB-INF/classes directory. Any unpacked custom classes and resources should be uploaded in the /html/WEB-INF/classes directory, while classes and resources packed in Jar files should be uploaded to /html/WEB-INF/lib."
uhh... so which is it? /classes? or /lib? I dont think they explain that very well and I'm a little confused by this statement. Also what exactly do I install? With a normal solr install, solr is put somewhere else, the war file is copied into tomcat and the rest of solr is referenced using some kind of XML configuration file.
Also, since I'm a little new to Java and servlets, can someone explain the tomcat file structure to me (in great detail will definitely get you a +1 from me) and where things should go and why?
Thanks in advance!
Web application structure is defined by J2EE spec, it's not limited (or specific) to Tomcat per se. Here is a detailed tutorial covering its layout. Briefly, however, it's as follows:
There a base (root, home, whatever you want to call it) folder which serves as root of web application, everything else goes under it.
All public stuff (html, images, CSS, javascript, JSP, what have you) goes under that folder (directly or via subfolders).
There's one special folder, also located directly under root, called WEB-INF. It contains non-public stuff, like application descriptor (web.xml), classes (which go into WEB-INF/classes folder), libraries (WEB-INF/lib) and possibly configuration files.
Application can be deployed either using expanded structure above or as WAR (web archive) which is basically an archive containing everything above starting at root folder level (but not including root).
The distinction between classes and lib folders is simple: all packaged libraries (JAR files) need to go into lib; all unpackaged classes (and resource files that need to be in classpath) have to go into classes preserving their directory structure (e.g. com.mypackage.Blah class should go into classes/com/mypackage/)
In your case, it looks like you can only have one web application deployed and it has to be deployed to /html folder. If you're deploying a war file, you need to extract it to that directory (e.g. from within that /html folder run jar xvf solr.war or whatever it's called).
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.