November 6, 2008

Using NetBeans HTTP monitor with Jetty and Maven

NetBeans provides a HTTP monitor we can use to inspect requests made to the server. When we run Tomcat from within NetBeans we can use this HTTP monitor, but what if we want to use Jetty? Jetty is not (yet) supported as a server in NetBeans, so how can we configure Jetty and NetBeans to use the HTTP monitor?

The help of NetBeans already has a section Setting up the HTTP monitor | To enable the HTTP Monitor for servers that are started outside the IDE. The basic steps are:

  • Add org-netbeans-modules-web-httpmonitor.jar and org-netbeans-modules-schema2beans.jar libraries to the web application.
  • Add a filter to the web.xml of the web application.
In this post we will see how we can achieve this for a Maven webapp project.

Suppose we have an existing Maven webapp project we want to enable for HTTP monitoring. First we create a new directory src/main/monitoring/WEB-INF/lib. In this directory we copy the file org-netbeans-modules-web-httpmonitor.jar from the directory IDE-install-directory/enterprise5/modules/ext/. And the file org-netbeans-modules-schema2beans.jar from the directory IDE-install-directory/ide9/modules/. Next we create the file src/main/monitoring/WEB-INF/web-monitoring.xml. In this file we only add the necessary filter configuration:

<?xml version="1.0" encoding="UTF-8"?>
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

Notice the value for the init-param element. Here we define on which machine and port the HTTP monitor of NetBeans is running. The port number is 8082 by default, but can change if it is already occupied by another application. In that case we must use a TCP view utility to get the correct port number. In the NetBeans help we can also see how we can configure multiple HTTP monitor instances. For this example we stick with the NetBeans HTTP monitor on our local machine.

And now comes Maven into play. We create a new profile in our pom.xml. In the profile we configure the Jetty and WAR plugins so we can use the HTTP monitor. Because we put it in a separate profile the main settings and deployments are not affected by the HTTP monitor setup.

The following section shows the profile:

        | Define a separate name, so it will not interfere with other
        | settings.
                | Copy WEB-INF/lib to webapp directory,
                | but exclude the web-monitoring.xml file. 
              | Jetty will merge the web-monitoring.xml file with
              | the main web.xml.

Now we can invoke mvn jetty:run -Pnetbeans-monitoring and once Jetty has started we can request pages and the NetBeans HTTP monitor shows them.