new File not found in Webcontent. Need to refresh - java

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.

Related

How to redeploy war file in tomcat without deleting/undeploy the old application?

I already have a working Tomcat Server with Web Manager where I can deploy my WAR file.
The WAR file contains my web application and this web application has a feature where user can upload images into one of it's folder.
The problem is when I need to update the web application using a new WAR file.
When I upload it through Tomcat manager. It says WAR file already exist.
I have to UNDEPLOY it first before I can upload the updated WAR.
I don't want to do this UNDEPLOY process because it will delete the application along with all the images that user already uploaded.
So, how can I update my web application without deleting those uploaded files?
Or maybe I was using the wrong approach with my application?
Since I need a quick fix. Here is what I did to make it work:
Create a temporary folder anywhere on the server lets say "/var/tmp/war_images". This will be used to temporarily store uploaded images when deploying a new war
Create a folder to put new WAR file. lets say "/var/tmp/war"
Create a shell script that do these things:
Copy all images from folder where user upload their images to the temp folder above
Delete application folder in tomcat
Delete WAR file in tomcat
Copy new WAR file from point 2 to tomcat8 folder
Restart tomcat (to trigger the deployment)
Copy back all images from point no.1 back to app folder newly created by the deployment process
So when I want to deploy a new WAR. I put the new WAR file on step no.2 and then execute the shell script.

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

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.

When tomcat redeploy, the previous upload files are missing

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.

Website directory structure different from eclipse

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.

Categories

Resources