Featured image of post Apache Tomcat Code Snippets

Apache Tomcat Code Snippets

1.

Starting Tomcat from the Terminal

Because clicking on icons is for the weak.

1
2
3
cd /path/to/tomcat/bin
./startup.sh  # Linux/Mac
startup.bat  # Windows

If this doesn’t work, check your Java installation… or just start praying.


2.

Stopping Tomcat Properly

Because kill -9 should not be your default debugging tool.

1
2
3
cd /path/to/tomcat/bin
./shutdown.sh  # Linux/Mac
shutdown.bat  # Windows

If Tomcat refuses to shut down, then you may proceed with extreme prejudice using kill -9.


3.

Changing the Default HTTP Port

Tired of port 8080?

Want to make your co-workers hate you by switching it to something else?

Edit conf/server.xml:

1
2
3
<Connector port="9090" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" />

Congratulations, you have now successfully confused everyone who uses your server.


4.

Enabling HTTPS on Tomcat

Because security is not optional (even if configuring it is a pain).

1
2
3
4
5
6
7
8
9
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
           maxThreads="200"
           SSLEnabled="true">
    <SSLHostConfig>
        <Certificate certificateFile="conf/your-cert.pem"
                     certificateKeyFile="conf/your-key.pem"
                     type="RSA" />
    </SSLHostConfig>
</Connector>

Don’t forget to restart Tomcat and sacrifice a small offering to the SSL gods.


5.

Deploying a WAR File Manually

Because automation is overrated.

1
cp myapp.war /path/to/tomcat/webapps/

Or, if you’re fancy, use Tomcat’s built-in manager (but who has time for GUIs?).


6.

Checking Active Tomcat Processes

For when you have no idea why Tomcat isn’t responding.

1
2
ps aux | grep tomcat  # Linux/Mac
tasklist | findstr tomcat  # Windows

If you see multiple instances, well… good luck.


7.

Setting Up a Basic DataSource for MySQL

Because hardcoding database credentials is for amateurs.

1
2
3
4
5
<Resource name="jdbc/MyDB" auth="Container"
          type="javax.sql.DataSource" driverClassName="com.mysql.cj.jdbc.Driver"
          url="jdbc:mysql://localhost:3306/mydb"
          username="root" password="password"
          maxTotal="20" maxIdle="10" maxWaitMillis="10000" />

Put this in conf/context.xml, restart Tomcat, and hope for the best.


8.

Rotating Logs Like a Pro

If your catalina.out is bigger than your hopes and dreams, it’s time for log rotation.

Create a new logrotate config:

1
vi /etc/logrotate.d/tomcat

Add this magic:

1
2
3
4
5
6
7
/path/to/tomcat/logs/catalina.out {
    copytruncate
    daily
    rotate 7
    compress
    missingok
}

Now your logs won’t take over your hard drive (hopefully).


9.

Setting JVM Options for Better Performance

Tomcat loves memory, so give it what it wants.

Edit setenv.sh or setenv.bat:

1
export CATALINA_OPTS="-Xms512m -Xmx2g -XX:+UseG1GC"

Because a well-fed Tomcat is a happy Tomcat.


10.

Reloading Context Without Restarting Tomcat

Because sometimes, you just need a quick refresh.

1
touch /path/to/tomcat/webapps/myapp/META-INF/context.xml

This will trigger a reload of the context without bringing the whole server down.

Magic!


Key Ideas

SlugSummary
10-apache-tomcat-code-snippetsUseful Tomcat code snippets for devs & admins
tomcat-httpsHow to enable HTTPS on Tomcat
tomcat-memory-configSetting JVM memory options for performance
tomcat-log-rotationKeeping Tomcat logs under control
tomcat-deploymentDeploying WAR files manually

References