2015-02-19

Helping NetBeans and Tomcat Get Along Together

I use NetBeans 8.0.2 with Tomcat 8.0.18 for developing web apps in Vaadin 7.

Generally NetBeans and Tomcat work well together. But I have learned how to workaround a pair of annoying problems.

Session Persistence


The first problem is session persistence. By default, Tomcat attempts to serialize all active sessions’ data. Serialization means extracting the data values from each session’s graph of objects and then writing those values to storage. Serializing sessions enables two uses: (a) re-create any remaining sessions when a server restarts, and (b) enable "clustering" where sessions can be moved between multiple Tomcat servers either for fail-over or for load-balancing.

Those sessions’ graph of objects includes all the objects in your Vaadin app. If you fail to make all your classes serializable, then Tomcat reports errors when shutting down and/or starting up.

If you don't care about (a) and (b) above, then you have no need for persistence of sessions and no need to bother making your app’s classes serializable. But how to stop Tomcat from trying and failing to persist sessions?

As described at the bottom of Tomcat’s Manager documentation page, you can add a single line to your "context.xml" file. That file was created for you by the Vaadin plugin or Maven archetype when your project was created. In a Maven multi-module project, look within the "-ui" submodule > Web Pages > META-INF > context.xml. Change this:

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/MyAppName-ui"/>

to this, adding a "Manager" tag:

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/MyAppName-ui">
    <Manager pathname="" />
</Context>

Multi-Launching


A more troublesome problem with NetBeans+Tomcat is that your Vaadin app is "double-launched". The app launches, shuts down, and launches again. At the very least this makes reading of the log confusing. Even worse, bad side-effects can happen especially if you launch daemon threads in the background. 

Let me be clear: This is only a development problem, a side-effect of combining NetBeans with Tomcat. In production with Tomcat alone, no problem.

The cure is deletion of an XML file that represents the running of your app. Every time NetBeans finishes running your app, delete that file before running again. That file is the name of your app plus ".xml", found in the path: "conf/Catalina/localhost" within your Tomcat's "Cataline Base" folder.

See this Question, Tomcat deploying the same application twice in netbeans, on StackOverflow.com for discussion. See my Answer for my version of others’ code that finds and deletes that .xml each time you stop running your Vaadin app from NetBeans.