Is there any way to configure tomcat servlet working directory? - java

When I'm running my web-application in tomcat and of course it's classes having tomcat working directory, because the JWM was started there. But is there any way to configure tomcat, as all classes of each deployed application, will have the application directory as their working directory?

You don't have to configure Tomcat working directory. You can get your application root directory from your servlet by calling :
String root = getServletContext().getRealPath("/");
Note that this folder is accessible from client browser.
If you want a working directory that is inaccessible from client, create a folder inside WEB-INF
String privateRoot = getServletContext().getRealPath("/WEB-INF");

You can change Tomcat's startup scripts. The tomcat process inherits whatever the current directory was for the startup script. To make tomcat start in a different working directory, you'll have to figure out what is launching tomcat, and change that process to change to the desired directory before it runs startup.sh.
You can look here on how to setup the working directory.

Related

System.getProperty("user.dir") gives different path in console and web application

I want to understand working of System.getProperty("user.dir") in different applications like console application , web application etc.
It's giving different path while running from console application and web application. Here are the examples-:
While running in console application it prints as below:
D:\eclipse workspace mars\ResearchProject
Which is root directory of project in which java class files lies having System.getProperty("user.dir") line of code.
On the other hand if I run System.getProperty("user.dir") code from some servlet/ service/ business/ dac java class then it prints as below:
D:\eclipse-jee-mars-2-win32\eclipse
Which is root folder of eclipse.
How System.getProperty("user.dir") works for different kind of application?
Is there any way to get root directory of web application?
How System.getProperty("user.dir") works for different kind of application?
It works exactly the same way in all cases. It returns the "current directory" that was specified by the application that launched the application.
In the case of an interactive shell, it will be the current directory of the shell (or subshell) that launched the application.
In the case of a shell script or a native launcher, it will be the current directory set by the script or the launcher. (For example, when you start Tomcat using the "catalina.sh" script, the script sets the current directory.)
In the case of an application launched by Eclipse, it will be the current directory set by the launch configuration you are using. The default is the project directory, but you can override this in the launch config.
Is there any way to get root directory of web application?
I assume that you mean the root directory of the deployed webapp in the web container.
String path = request.getServletContext().getRealPath("/");
For more details:
Get the root directory name in Java web application if context and root names are different
However, there are various "traps" with accessing webapp files via the file system. One is that they may be clobbered at any time by a redeployment. A better approach is to access "files" in the deployed webapp using getResourceAsStream.

Starting .war file from another war in Tomcat 8

Is there a way to start/stop .war file from inside another .war?
I'm running SymmetricDS server using Tomcat 8 (it deploys it's own .war) and I need to start it, when a button is pressed and Stop likewise. Can I do that?
When a war file is "dropped" into Tomcat's webapp folder, by default Tomcat automatically deploys it and starts it. When source war file is deleted from the webapp folder, Tomcat automatically stops and undeploys the webapp.
So basically all you need to do is copy and delete the war file to/from the webapp folder to start/stop a webapplication.
Also (or if you can't do this) Tomcat has a built-in manager webapplication which is capable to deploy new applications (from war-files), or to stop and undeploy running web applications.
See Manager App HOW-TO for more details on this.
If you want to do this from your code, check out the ManagerServlet class. You can call it with simple URLs and parameters. The javadoc of the class contains example URLs what you can do with it. Here are 2 important operations specifically to your needs:
/start?path=/xxx - Start the web application attached to context path /xxx for this virtual host.
/stop?path=/xxx - Stop the web application attached to context path /xxx for this virtual host.

How to change path for web project in eclipse

I wrote the web application using Spring MVC. When I deploy the app to the server path is localhost:8080/projectName/. It is possible to remove projectName from path from eclipse? I found path in project properties but I cannot find the way how to change it.
I found that I have to change context root to : "/". I changed it but that has no effect.
Thank you for any help.
Regards,
Sebastian
I would break down your question into two parts :
A. Changes to the context root not taking any effect.
For changes to the context root to take effect, you must clean and republish you webapp on your server for the context root changes to get activated. To run "Clean" in context of the server from within eclipse,
Stop the Server
Window -> Show View -> Server -> Right click on your server configuration -> Clean. (Note: To "Clean" the server outside of eclipse, you need to go to the "webapps" directory of your server on your local filesystem and delete the .war file as well as the "project-name" folder which holds the exploded WAR file. )
Deploy your webapp to the server and restart the server.
B. Trying to run your webapp at the context root / - localhost:8080
From what it looks like, you are trying to run your web application at the "Root" of your application server. You haven't mentioned the application server that you are using, but let us for example assume that the server you are using is tomcat. For tomcat, to deploy an application which will run at localhost:8080/ you need to either deploy the exploded war under the "ROOT" directory at $CATALINA_HOME/webapps/ROOT , or name your war file to be root.war.
The $CATALINA_HOME/webapps/ROOT and $CATALINA_HOME/webapps/root.war are special keywords which tell tomcat to deploy the application at content root /.
If you are NOT using tomcat as your application server, then provide more details on the application server being used to see if I can help.

can a deployed war/application be edited?

I have an application/war deployed in server. Now at runtime I want to add an xml document to the war/application. can I do that? if yes, what is the path of an war/application for it to be added.
You have to repackage the WAR, redeploy, and bounce the server. It's not that simple.
You can make that data available without the hassle if you put it in a database and have your application access it there.
in the webapps/WEB-INF folder you can find the xml files
It depends on the servlet container/application server.
If you are using Tomcat, wars are getting unpacked to the webapps directory inside the Tomcat directory.
Move webapps/app directory outside the webapps directory, what will cause an undeploy of app
Put your xml file into the app directory
Move app back to webapps what will cause a deploy

How to deploy a Java Web Application (.war) on tomcat?

I have a .war file of a Java Web Application. Now I want to upload it to my ftp server so that I can execute it.
What steps I should perform to run it?
The context path of the webapp is /mywebapp
Edit
Actually, my ftp server name is ftp://bilgin.ath.cx/ and I have uploaded my TestWebApp.war file to this dir: ftp://bilgin.ath.cx/web
Then what should be the URL to access the index.html page of the webapplication
#2 Edit
Tomcat is listening on 8082
Apache access Tomcat with jk connector.
As others pointed out, the most straightforward way to deploy a WAR is to copy it to the webapps of the Tomcat install. Another option would be to use the manager application if it is installed (this is not always the case), if it's properly configured (i.e. if you have the credentials of a user assigned to the appropriate group) and if it you can access it over an insecure network like Internet (but this is very unlikely and you didn't mention any VPN access). So this leaves you with the webappdirectory.
Now, if Tomcat is installed and running on bilgin.ath.cx (as this is the machine where you uploaded the files), I noticed that Apache is listening to port 80 on that machien so I would bet that Tomcat is not directly exposed and that requests have to go through Apache. In that case, I think that deploying a new webapp and making it visible to the Internet will involve the edit of Apache configuration files (mod_jk?, mod_proxy?). You should either give us more details or discuss this with your hosting provider.
Update: As expected, the bilgin.ath.cx is using Apache Tomcat + Apache HTTPD + mod_jk. The configuration usually involves two files: the worker.properties file to configure the workers and the httpd.conf for Apache. Now, without seeing the current configuration, it's not easy to give a definitive answer but, basically, you may have to add a JkMount directive in Apache httpd.conf for your new webapp1. Refer to the mod_jk documentation, it has a simple configuration example. Note that modifying httpd.conf will require access to (obviously) and proper rights and that you'll have to restart Apache after the modifications.
1 I don't think you'll need to define a new worker if you are deploying to an already used Tomcat instance, especially if this sounds like Chinese for you :)
copy the .war file in the webapps folder
upload the file using the manager application - http://host:port/manager. You will have to setup some users beforehand.
(not recommended, but working) - manually extract the .war file as a .zip archive and place the extracted files in webapps/webappname
Sometimes administrators configure tomcat so that war files are deployed outside the tomcat folder. Even in that case:
After you have it deployed (check the /logs dir for any problems), it should be accessible via: http://host:port/yourwebappname/. So in your case, one of those:
http://bilgin.ath.cx/TestWebApp/
http://bilgin.ath.cx:8080/TestWebApp/
If you don't manage by doing the above and googling - turn to your support. There might be an alternative port, or there might be something wrong with the application (and therefore in the logs)
The tomcat manual says:
Copy the web application archive file into directory $CATALINA_HOME/webapps/. When Tomcat is started, it will automatically expand the web application archive file into its unpacked form, and execute the application that way.
Note that you can deploy remotely using HTTP.
http://localhost:8080/manager/deploy
Upload the web application archive
(WAR) file that is specified as the
request data in this HTTP PUT request,
install it into the appBase directory
of our corresponding virtual host, and
start it using the war file name
without the .war extension as the
path. The application can later be
undeployed (and the corresponding
application directory removed) by use
of the /undeploy. To deploy the ROOT
web application (the application with
a context path of "/"), name the war
ROOT.war.
and if you're using Ant you can do this using Tomcat Ant tasks (perhaps following a successful build).
To determine which path you then hit on your browser, you need to know the port Tomcat is running on, the context and your servlet path. See here for more details.
Log in :URL = "localhost:8080/"
Enter username and pass word
Click Manager App
Scroll Down and find "WAR file to deploy"
Chose file and click deploy
Done
Go to Webapp folder of you Apache tomcat you will see a folder name matching with your war file name.
Type link in your url address bar:: localhost:8080/HelloWorld/HelloWorld.html and press enter
Done

Categories