Search

Dark theme | Light theme

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"?>
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
    <filter>
        <filter-name>HTTPMonitorFilter</filter-name>
        <filter-class>org.netbeans.modules.web.monitor.server.MonitorFilter</filter-class>
        <init-param>
            <param-name>netbeans.monitor.ide</param-name>
            <param-value>localhost:8082</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>HTTPMonitorFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

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:

<profile>
  <id>netbeans-monitoring</id>
  <build>
    <!--+ 
        | Define a separate name, so it will not interfere with other
        | settings.
        +-->
    <finalName>monitor-${pom.artifactId}-${pom.version}</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <configuration>
          <webResources>
            <!--+ 
                | Copy WEB-INF/lib to webapp directory,
                | but exclude the web-monitoring.xml file. 
                +-->
            <resource>             
              <directory>src/main/monitoring</directory>
              <excludes>
                <exclude>**/*.xml</exclude>
              </excludes>
            </resource>
          </webResources>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>6.1.11</version>
        <configuration>
          <!--+ 
              | Jetty will merge the web-monitoring.xml file with
              | the main web.xml.
              +-->
          <webAppConfig>
            <overrideDescriptor>src/main/monitor/WEB-INF/web-monitoring.xml</overrideDescriptor>
          </webAppConfig>
        </configuration>
      </plugin>
    </plugins>
  </build>
</profile>

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.