Dear Wiki user, You have subscribed to a wiki page or wiki category on "Tomcat Wiki" for change notification.
The "HowTo" page has been changed by ChuckCaldarale. The comment on this change is: Clarify default servlet usage.. http://wiki.apache.org/tomcat/HowTo?action=diff&rev1=91&rev2=92 -------------------------------------------------- == How do I load a properties file? == Here are the three most popular ways:: + * Use a classloader's getResource to get an url to the properties file and load it into the Properties. The properties file must be located within the webapp classpath (i.e. either WEB-INF/classes/... or in a jar. + A challenge is to get the classloader when you are in a static initializer: + {{{ public class Config { private static java.util.Properties prop = new java.util.Properties(); @@ -32, +35 @@ // get class loader ClassLoader loader = Config.class.getClassLoader(); if(loader==null) - loader = ClassLoader.getSystemClassLoader(); + loader = ClassLoader.getSystemClassLoader(); // assuming you want to load application.properties located in WEB-INF/classes/conf/ String propFile = "conf/application.properties"; java.net.URL url = loader.getResource(propFile); - try{prop.load(url.openStream());}catch(Exception e){System.err.println("Could not load configuration file: " + propFile);} + try{prop.load(url.openStream());}catch(Exception e){System.err.println("Could not load configuration file: " + propFile);} } //.... @@ -49, +52 @@ } } }}} - This method even works in a standalone java application. So it is my preferred way. (see also [[http://knowhow.amazers.net/space/dev/java+tips/Loading+properties+in+a+web+application|this article]]) * Use a `ResourceBundle`. See the Java docs for the specifics of how the `ResourceBundle` class works. Using this method, the properties file must go into the `WEB-INF/classes` directory or in a jar file contained in the `WEB-INF/lib` directory. @@ -830, +832 @@ == How do I make my web application be the Tomcat default application ? == Congratulations. You have created and tested a first web application (traditionally called "mywebapp"), users can access it via the URL "http://myhost.company.com/mywebapp". You are very proud and satisfied. But now, how do you change the setup, so that "mywebapp" gets called when the user enters the URL "http://myhost.company.com" ? - The pages and code of your "mywebapp" application currently reside in (CATALINA_BASE)/webapps/mywebapp/. In a standard Tomcat installation, you will notice that under the same directory (CATALINA_BASE)/webapps/, there is a directory called ROOT (the capitals are important, even under Windows). That is the residence of the ''current'' Tomcat default application, the one that is called right now when a user calls up "http://myhost.company.com[:port]". The trick is to put your application in it's place. + The pages and code of your "mywebapp" application currently reside in (CATALINA_BASE)/webapps/mywebapp/. In a standard Tomcat installation, you will notice that under the same directory (CATALINA_BASE)/webapps/, there is a directory called ROOT (the capitals are important, even under Windows). That is the residence of the ''current'' Tomcat default application, the one that is called right now when a user calls up "http://myhost.company.com[:port]". The trick is to put your application in its place. First stop Tomcat.<<BR>> Then before you replace the current default application, it may be a good idea to make a copy of it somewhere else.<<BR>> Then delete everything under the ROOT directory, and move everything that was previously under the (CATALINA_BASE)/webapps/mywebapp/ directory, toward this (CATALINA_BASE)/webapps/ROOT directory. In other words, what was previously .../mywebapp/WEB-INF should now be .../ROOT/WEB-INF (and not .../ROOT/mywebapp/WEB-INF). Just by doing this, you have already made you webapp into the Tomcat ''default webapp''. - One step is left : you also need to have, within your application, a ''default servlet''. This, you do by means of an appropriate url-mapping in the WEB-INF/web.xml configuration file of your application. Make sure you have something like this in that file : + One step is left : you also need to have, within your application, a ''default servlet''. If you don't want to use the standard one supplied by Tomcat that does nothing but deliver static content, you'll need to supply one of your own. This you do by means of an appropriate url-mapping in the WEB-INF/web.xml configuration file of your application. Make sure you have something like this in that file: {{{ <servlet> @@ -849, +851 @@ <url-pattern>/*</url-pattern> </servlet-mapping> }}} + The above will override the mapping for Tomcat's DefaultServlet in the global conf/web.xml file. + Restart Tomcat and you're done.<<BR>> Call up "http://myhost.company.com/" and enjoy. '''Addendum 1 : If you are deploying your application as a war file..''' @@ -900, +904 @@ This will produce a thread dump on standard output, but may not be possible to capture to a file. == How do I use Hibernate and database connection pooling with Tomcat? == - See [[TomcatHibernate]] + See TomcatHibernate == How do I set up Tomcat virtual hosts in a development environment? == - See [[TomcatDevelopmentVirtualHosts]] + See TomcatDevelopmentVirtualHosts + ---- [[CategoryFAQ]] --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org