Skip to content

Instantly share code, notes, and snippets.

@BruceZu
Last active June 7, 2018 06:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BruceZu/3952e52edecfd3ce4b224b897641ea32 to your computer and use it in GitHub Desktop.
Save BruceZu/3952e52edecfd3ce4b224b897641ea32 to your computer and use it in GitHub Desktop.
Tomcat

Tomcat 8

https://tomcat.apache.org/tomcat-8.5-doc/host-manager-howto.html https://tomcat.apache.org/tomcat-8.5-doc/config/host.html

Automatic Application Deployment

https://tomcat.apache.org/tomcat-5.5-doc/config/host.html#Automatic Application Deployment

Tips

http://www.onjava.com/pub/a/onjava/2003/06/25/tomcat_tips.html

Apache Tomcat wiki

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").
@BruceZu
Copy link
Author

BruceZu commented Apr 11, 2018

ResourceResolver myResolver = new ResourceResolver() {
     public InputStream getResourceAsStream(String resourceName) {
         ClassLoader loader = Thread.currentThread().getContextClassLoader();
         if (loader == null) { // there may not be a context class loader
             loader = MyClazz.class.getClassLoader();
         }
         return loader.getResourceAsStream(resourceName);
     }
 };

@BruceZu
Copy link
Author

BruceZu commented Apr 11, 2018

Code:
myclass:

this.getClass().getClassLoader().getResourceAsStream("config.xml")

to get a configuration out of the classpath.

It worked really well, but now I found something weird:

I load the class with the above function in a jsp,
the class residing in a jar in the tomcat/common/lib dir and
the config.xml in the webroot/WEB-INF/classes
Suddenly the config.xml cannt be loaded any more... at least not by myclass.

Thread.currentThread().getContextClassLoader().getResourceAsStream("config.xml")

Tomcat sets the context class loader to the class loader of the webapp (which can of course load stuff from WEB_INF/classes).
The original line this.getClass().getClassLoader().getResourceAsStream("config.xml") does not work when the class that contains the line lives in common/lib or shared/lib, because its class loader is then the 'common' or 'shared' class loader, which cannot see into individual webapps.
Class Loader HOW-TO http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment