So, the inverse of this question has been asked before and answered here: How to set the context path of a web application in Tomcat 7.0. However, my application is deployed as "ROOT" and I need it to be available at "my-path" instead of deploying it at "my-path" and needing it available at "ROOT". I'm trying out Amazon's Elastic Beanstalk offering and a deployed war will always go to ROOT. I don't have any control over this and there is not .war file left behind. I tried to aforementioned topic to solve my problem, but it seems that pointing ROOT to another path doesn't work, while pointing another path to ROOT does work.
I will have to create an AMI so that auto scaling can take place without me touching new instances. The only thing I have been able to do to get this working was to create a symbolic link in the webapps folder that points "my-path" to "ROOT." I have no idea if there are significant repercussions of this setup and would like to hear if there are and what alternative there may be that makes use of Tomcat's settings instead or even another non-Tomcat solution.
Thanks!
Update: Once I created the AWS AMI with a symbolic link in the webapps folder and actually changed the AMI in Elastic Beanstalk, I found that my original solution will not work because Beanstalk wipes out the entire webapps directory.
One way to do this would be to deploy the application as ROOT but configure it so all the resources are under the path /my-app in the ROOT web application.
For an existing application deployed as ROOT this would require:
Add a "my-app" directory to the root of the web application and move all your static resources from the root of the web application in to the "my-app" directory.
Add the path "/my-app" to the beginning of all the URL paths in your web.xml and/or annotations
Related
I suppose same problem as described in question MSAL for Java quickstart sample app throws exception. When using IDE and deploying to embedded tomcat, app works. After spending one day, I figured out what is the problem.
Application AuthPageController specifies #RequestMapping("/msal4jsample/secure/aad"). This works with embedded tomcat. When deployed to tomcat server, tomcat removes application name from path since it is deployment information and exploded folder name depends on war file name. Same application can be deployed multiple times to different folders. Tomcat maps url as /secure/aad and request in this case is never handled. To fix problem, I created array of request mappings #RequestMapping(value = {"/msal4jsample/secure/aad", "/secure/aad"}).
I forked MS Azure repository and made changes. Please take a look at zdenko-s/ms-identity-java-webapp
There are other fixes too.
War file name is specified in pom.xml, no need to rename it. Removed rename step from documentation also
.gitignore wrongly specifies exclude target. Should be */target
Sharing info. Fix in my forked branch
Ideally what I want to be able to do is load in a fresh copy of the config if a change is detected so that if it is updated in the DB, it gets fetched and updated in the application without a restart.
I tried to add autodeploy = true inside host, server.xml and reloadable = true inside context.xml.But none of this worked.
Is there any other solution for this. Iam using eclipse IDE and my sever is Tomcat.
I read adding inside context will help for this.
<Context reloadable="true">
<!-- Default set of monitored resources -->
<WatchedResource>Config/Design/configs/globalconfig</WatchedResource>
But this one still din't help me.Am I giving the path in wrong way or something?I got this from the following link https://www.mulesoft.com/tcat/tomcat-reload
Edited Besides I tried with the auto reload on module, disabled and enabled as well. And also noting one more thing here I don't want to use JRebel.
If I go by the heading of the question
change in configuration/setting without restarting Tomcat
I would agree with Jomcy Johny, that the design approach perhaps need to be reviewed. In continuation to the above comment, perhaps you should consider keeping the configuration outside of the container. There are many way of achieving the same, one possible direction is with Apache Zookeeper.
On the side note, the path mentioned in the Mule document is generally of form 'WEB-INF/x/yz' or '/Dir0/config.file'.
It is possible to deploy web applications to a running Tomcat server.
If the Host autoDeploy attribute is "true", the Host will attempt to deploy and update web applications dynamically, as needed, for example if a new .WAR is dropped into the appBase. For this to work, the Host needs to have background processing enabled which is the default configuration.
autoDeploy set to "true" and a running Tomcat allows for:
Deployment of .WAR files copied into the Host appBase.
Deployment of exploded web applications which are copied into the
Host appBase.
Re-deployment of a web application which has already been deployed
from a .WAR when the new .WAR is provided. In this case the exploded
web application is removed, and the .WAR is expanded again. Note that
the explosion will not occur if the Host is configured so that .WARs
are not exploded with a unpackWARs attribute set to "false", in which
case the web application will be simply redeployed as a compressed
archive.
Re-deployment of a web application if the /WEB-INF/web.xml file (or
any other resource defined as a WatchedResource) is updated.
Re-deployment of a web application if the Context Descriptor file
from which the web application has been deployed is updated.
Re-deployment of a web application if a Context Descriptor file (with
a filename corresponding to the Context path of the previously
deployed web application) is added to the
$CATALINA_HOME/conf/[enginename]/[hostname]/ directory.
Undeployment of a web application if its document base (docBase) is
deleted. Note that on Windows, this assumes that anti-locking
features (see Context configuration) are enabled, otherwise it is not
possible to delete the resources of a running web application
Above is a snippet from Apache tomcat And also most of the general idea about hot deployment I gained from hot deploy
Reloading Context elements when they are within the server.xml is not likely to work as "...server.xml file cannot be reloaded without restarting Tomcat"[1].
What you can try, is creating a file at "/META-INF/context.xml"[1], and adding the context in there.
Then you can replace the war file if with the new context files without a server restart.
Or you program a server restart in if needed. See [2]
source:
[1] https://tomcat.apache.org/tomcat-8.5-doc/config/context#Defining_a_context
[2] Java - Tomcat: Reload context.xml without restarting server
You can write a script and run it as a service outside your application to keep a track of your config file's last modified time, in case of a change you switch to your Tomcat container and run the following command (considering OS is Linux):
source ~/.bashrc
(In case of windows, its done as soon as you change environment variables and save.)
Hope that this helps.
I have added tomcat 7 to my eclipse.
When I start the server it gets started successfully(shows the message in console) but when I hit the URL http://localhost:8080 it says "404 resource not found".
Strange thing is when I go to Tomcat bin directory and start it through start.bat, the same url http://localhost:8080 shows the tomcat homepage in browser without any issue.
Any idea? Help is appreciated.
You need to be aware of two directory trees with Tomcat : TOMCAT_HOME and TOMCAT_BASE.
TOMCAT_HOME contains binary related data and can be shared by many running Tomcat instances (it contains jar, dll, exe and other non-contextual data).
TOMCAT_BASE specifies configuration files (ie catalina.properties, server.xml, context configurations), webapp directories and further files such as specific librairies if required.
When running Tomcat from his installation directory: TOMCAT_HOME and TOMCAT_BASE are set to same location. Then, Tomcat serves ${TOMCAT_HOME}\webapps directory. The special ROOT context let serve the root path and by default contains a simple webapp (browse ${TOMCAT_HOME}\webapps\ROOT for more information).
When running Tomcat from Eclipse, Eclipse will setup two directories:
with editable configuration (generally goes under ${WORKSPACE}\Servers\${SERVER_NAME}-conf)
with deployed files from last describe directory, served context configuration, deployed context and some other working files such as log and work directories.
This last directory generally goes under your workspace metadata but you can edit it from the server editor. And Eclipse uses this directory as TOMCAT_BASE. To this directory, Eclipse doesn't deploy the default ROOT webapp neither other default ones (docs, examples, host-manager and manager). If necessary you can add them from the server configuration directory (first one described).
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.
I have this folder under Tomcat webapps/mysite which is where all my JSPs and other things are located. To access this folder I go to http://blah.com/mysite and it works just fine. However (because of stylesheets and images statically connected to the root /) I have to make it so that when I go to http://blah.com/ it will load the stuff inside webapps/mysite.
I've tried many different things including contexts and setting the absolute path in server.xml... nothing seems to work, whenever I go to http://blah.com/ it still tries to load the ROOT folder... what's happening here?
The solution I use is to set this in your Tomcat server.xml
Add a <Context> element within the <Host> like below which sets your mysite as the default web app. Note the empty path="" which makes it the default.
<Context docBase="mysite" path="" />
Attributes of Context Container from the Tomcat docs:
docBase You may specify an absolute pathname for this directory or WAR file, or a pathname that is relative to the appBase directory
of the owning Host.
path All of the context paths within a particular Host must be unique. If you specify a context path of an empty string (""), you are
defining the default web application for this Host, which will process
all requests not assigned to other Contexts.
See others who have had similar question and the similar answer here, here and here
See also Apache Tomcat Configuration Reference - Context
There are a number of ways to make an application the root application. The simplest way is to just replace the contents of webapps/ROOT with the contents of your web application.
For other solutions, please see the following website:
http://wiki.apache.org/tomcat/HowTo#How_do_I_make_my_web_application_be_the_Tomcat_default_application_.3F
https://stackoverflow.com/users/1123501/george-siggouroglou 's awnser works but lacks a step.
delete ROOT and all items
copy the war to webapps as ROOT.war
Without the deletion, it may not work. Tested with docker.
You can rename your war from something.war == to ==> ROOT.war.
So, tomcat will unpack the war and will create the folder ROOT for it.
It is a trick that is working on tomcat 8 also.