Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Removed all content of the page. The up-to-date version of all this is /HowTo.

This page is obsolete. See HowTo for the up-to-date version.

...

CategoryObsolete

How do I load a properties file?

Here are 2 most popular ways::

1) 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.

2) Another way is to use the method getResourceAsStream() from the ServletContext class. This allows you update the file without having to reload the webapp as required by the first method. Typical code snippet, without all the try catches:

No Format
    // Assuming you are in a Servlet extending HttpServlet 
    // This will look for a file called "/more/cowbell.properties" relative 
    // to your servlet Root Context 
    InputStream is = getServletContext().getResourceAsStream("/more/cowbell.properties"); 
    Properties  p  = new Properties(); 
    p.load(is); 
    is.close();  

How do I use log4j for all Tomcat log output?

To have Tomcat use log4j universally, place both log4j.jar and the Jakarta commons-logging.jar into the $TOMCAT_HOME/common/lib directory. Create your log4j properties file as $TOMCAT_HOME/common/classes/log4j.properties and configure the root logger. Here is the basic log4j.properties I used for this myself:

No Format

log4j.rootLogger=info, R
log4j.appender.R=org.apache.log4j.ConsoleAppender
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%-5p %-30.30c{1} %x - %m%n

If you only need to use log4j in your own web app, just include log4j (and any log4j properties file) in your WAR.

(I could not find this in any FAQ or documentation as of Feb 11 04, but it has been discussed several times on the mailing list.)

How do I configure the Connectors?

On the FAQ there are links to Other Resources which should have a relevant link to your config. If not, there are the JK docs. The JK docs for 3, 4, and 5 are all virtually the same. JK2 is not quite ready for production. (YMMV) Lucky - there is a new committer which has been committing a lot of good jk2 patches so this statement may be outdated. (As of Dec 1, 2003 its not)

Tomcat Crashed What do I do?

These steps are in no particular order ...

...

TomcatWeb also has some short step-by-step Tomcat - Webserver interfacing documentation.

No Format
   }}}
----

''' How do I share sessions across webapps?'''

You cannot share sessions directly across webapps, as that would
be a violation of the Servlet Specification.  There are workarounds,
including using a singleton class loaded from the common classloader
repository to hold shared information, or putting some of this shared
information in a database or another data store.  Some of these approaches
have been discussed on the tomcat-user mailing list, whose archives
you should search for more information.

Sharing sessions across contains for clustering or replication is
a different matter altogether.

----

''' Why doesn't the netbeans example build for me? '''

I have found 2 issues with the build.xml provided here:
http://jakarta.apache.org/struts/faqs/netbeans.html

First, add this to the top of your properties:
<property environment="env" />

Next find this line in the "war" target:
<classes dir="${build.dir}" includes="**/*.properties" />

Change it so it reads like this:
<classes dir="${src.dir}" includes="**/*.properties" />

Now it will work!

----

'''I'm encountering classloader problems when using JNI under Tomcat'''

The important thing to know about using JNI under Tomcat is that one cannot place the native libraries OR their JNI interfaces under the WEB-INF/lib or WEB-INF/classes directories of a web application and expect to be able to reload the webapp without restarting the server. The class that calls System.loadLibrary(String) must be loaded by a classloader that is not affected by reloading the web application itself.

Thus, if you have JNI code that follows the convention of including a static initilaizer like this:

{{{  class FooWrapper { 
    static { 
        System.loadLibrary("foo"); 
    } 

    native void doFoo(); 
  } 

then both this class and the shared library should be placed in the $CATALINA_HOME/shared/lib directory.

Note that under Windows, you'll also need to make sure that the library is in the java.library.path. Either add %CATALINA_HOME%\shared\lib to your Windows PATH environment variable, or place the DLL files in another location that is currently on the java.library.path. There may be a similar requirement for UNIX based system (I haven't checked), in which case you'd also have to add $CATALINA_HOME/shared/lib to the PATH environment variable. (Note: I'm not the original author of this entry.)

The symptom of this problem that I encountered looked something like this -

No Format
    java.lang.UnsatisfiedLinkError: Native Library WEB-INF/lib/libfoo.so already loaded in another classloader 
        at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1525) 

Setting up SSL - from the tomcat user list archives

with Verisign:

http://marc.theaimsgroup.com/?l=tomcat-user&m=106285452711698&w=2

and :

http://marc.theaimsgroup.com/?l=tomcat-user&m=107584265122914&w=2

Wiki Markup
with \["OpenSSL"\]:

http://marc.theaimsgroup.com/?l=tomcat-user&m=106293430225790&w=2

or this:

http://marc.theaimsgroup.com/?l=tomcat-user&m=106453566416102&w=2

and this:

http://marc.theaimsgroup.com/?l=tomcat-user&m=106621232531781&w=2

Plus, "what SSL is all about anyway":

http://marc.theaimsgroup.com/?l=tomcat-user&m=106692394104667&w=2

If you're looking for a certicate provider, check here: http://www.sslreview.com/

How do I install Tomcat as a service under Unix

No Format

 #!/bin/sh
 # chkconfig: - 85 15
 # description: Tomcat is a servlet container
 PROGRAM=$CATALINA_HOME/bin/catalina.sh
 if [ -f $PROGRAM ]; then
        echo "$1ing" `basename $0`
        su - tomcat -c "$PROGRAM $1"
 fi

How do I install Tomcat as a service under Windows?

Wiki Markup
*How to UTF-8 your site?* see \["Tomcat/UTF-8"\]

How do I add a question to this page?

Anyone may edit this page to add their own content. That is why this page is part of a Wiki and not a hardcoded static file in the FAQ.

Wiki Markup
But do not add questions without answers to this page. If you have a question about how to do something in tomcat which has not been addressed yet, ask the tomcat-user list: \[http://jakarta.apache.org/tomcat/faq/tomcatuser.html \] Once you've figured out how to fix your problem, come back and update the Wiki to allow the rest of us to benefit from what you've learned!

How can I access members of a custom Realm or Principal? When you create a custom subclass of RealmBase or GenericPrincipal and attempt to use those classes in your webapp code, you'll probably have problems with ClassCastException. This is because the instance returned by request.getUserPrincipal() is of a class loaded by the server's ClassLoader, and you are trying to access it through you webapp's ClassLoader. While the classes maybe otherwise exactly the same, different (sibling) ClassLoaders makes them different classes.

This assumes you created a MyUserPrincipal class, and put in Tomcat's server\classes directory, as well as in your webapp's webinf\classes directory.