- https://www.mulesoft.com/tcat/tomcat-configuration
- https://www.mulesoft.com/tcat/understanding-apache-tomcat
https://tomcat.apache.org/tomcat-8.5-doc/host-manager-howto.html https://tomcat.apache.org/tomcat-8.5-doc/config/host.html
https://tomcat.apache.org/tomcat-5.5-doc/config/host.html#Automatic Application Deployment
http://www.onjava.com/pub/a/onjava/2003/06/25/tomcat_tips.html
Removing the unpackWARs feature https://wiki.apache.org/tomcat/RemoveUnpackWARs
// unpackWARs="false" the
SevletContext.getRealPath();// method always returns null
https://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String)
getServletContext().getRealPath("/")
// returns '\' at the end when I run my project in Tomcat 7 whereas it is not
// working as such in Tomcat 8.
// For example,
// In Tomcat 7 it returns as "D:\Tomcat\webapps\project\"
// In Tomcat 8 it returns as "D:\Tomcat\webapps\project"
//At present the project is in production so, I am unable to change the code
//in every part(where i use getRealPath("/")). Is there a way/setting in
//tomcat level configuration to make it resolved.
//Additional information, Tomcat version : 8.0.14
tomcat implementation
https://bz.apache.org/bugzilla/show_bug.cgi?id=57556
// the servlet-2_3-fcs-docs for ServletContext.getRealPath():
// "This method returns null if the servlet container cannot translate the virtual path to a real path for any reason
// (such as when the content is being made available from a .war archive)."
config.getServletContext().getRealPath("/" + storageLocation);
getServletContext().getResource("files");
// Read the javadoc for ServletContext.getResource:
//"Returns a URL to the resource that is mapped to a specified path. The
//path must begin with a "/" and is interpreted as relative to the current
//context root."
ServletContext#getRealPath()
// may return null in case that the cms.war is running without being unpacked.
// So, I think it's better to use a default storage directory instead in this
// case under the current working directory (e.g, "./repository_storage").
Runtime configuration
where to save the configured file
add its path to the classpath of the appserver.
assume to use Tomcat,
shared.loader
property ofTomcat/conf/catalina.properties
.https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html
how about other OS
how to get it
1. put it in classpath
foo.properties
is supposed to be placed in one of the roots which are covered by the default classpath of a webapp, e.g.webapp's
/WEB-INF/lib
and/WEB-INF/classes
,server's
/lib
,JDK/JRE's
/lib
.If the propertiesfile is webapp-specific, best is to place it in
/WEB-INF/classes
.If you have placed the
foo.properties
it in a Java package structure likecom.example
, then you need to load it as belowNote that this path of a context class loader should not start with a
/
. Only when you're using a "relative" class loader such asSomeClass.class.getClassLoader()
, then you indeed need to start it with a/
.the visibility of the properties file depends then on the class loader in question. It's only visible to the same class loader as the one which loaded the class. So, if the class is loaded by e.g. server common classloader instead of webapp classloader, and the properties file is inside webapp itself, then it's invisible.
The context class loader is your safest bet so you can place the properties file "everywhere" in the classpath and/or you intend to be able to override a server-provided one from the webapp on.
2. put it in webcontent
load it by ServletContext#getResourceAsStream() with a webcontent-relative path:
http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/ServletContext.html#getResource(java.lang.String)
3. Put it in local disk file system
So that you can load it the usual
java.io
way with an absolute local disk file system path:Note the importance of using an absolute path.
Relative local disk file system paths are an absolute no-go in a Java EE web application. See also the first "See also" link below.
Which to choose?
If you prefer being able to edit properties files from outside the web application without the need to rebuild and redeploy the WAR every time, then put it in the classpath outside the project (if necessary add the directory to the classpath).
If you prefer being able to edit properties files programmatically from inside the web application using
Properties#store()
method, put it outside the web application. As theProperties#store()
requires aWriter
, you can't go around using a disk file system path. That path can in turn be passed to the web application as a VM argument or system property. As a precaution, never usegetRealPath()
. All changes in deploy folder will get lost on a redeploy for the simple reason that the changes are not reflected back in original WAR file.3 class loader difference: context class loader; server common classloader and webapp classloader???
Get resouce
java.io.File
The
java.io.File
and consorts acts on the local disk file system.Relative paths in
java.io
are dependent on the current working directory.I.e.
the directory from which the JVM is started. This may for example be
C:\Tomcat\bin
or something entirely different.In a normal Eclipse project, that would be
C:\Eclipse\workspace\projectname
.but thus not
C:\Tomcat\webapps\contextname
or whatever you'd expect it to be.You can learn about the current working directory the following way:
cons: The working directory is in no way programmatically controllable. You should really prefer using absolute paths in the
File
API instead of relative paths. E.g.C:\full\path\to\file.ext
.cons: You don't want to hardcode or guess the absolute path in Java (web)applications. That's only portability trouble (i.e. it runs in system X, but not in system Y).
put them in classpath
place those kind of resources in the classpath, or to add its full path to the classpath (in an IDE like Eclipse that's the
src
folder and the "build path" respectively).locate files relative to the "root" of the classpath
In web applications (or any other application which uses multiple classloaders) it's recommend to use the
for this so you can look "outside" the webapp context as well.
alternative in webapps is the
It is able to access files located in the public webfolder of the webapp project, including the /WEB-INF folder.
ServletContext#getResource()
and its counterpartServletContext#getResourceAsStream()
. It is able to access files located in the publicweb
folder of the webapp project, including the/WEB-INF
folder. TheServletContext
is available in servlets by the inheritedgetServletContext()
method, you can call it as-is.Different ??
Save or Update at runtime
The public web content folder will all get lost whenever the webapp get redeployed or even when the server get restarted with a cleanup.
location outside the webapp deploy folder as a more permanent storage, so that it will remain intact across multiple deployments/restarts. prepare a fixed local disk file system folder
https://stackoverflow.com/questions/2161054/where-to-place-and-how-to-read-configuration-resource-files-in-servlet-based-app