Tuesday Apr 19, 2005

Monitor HTTP Requests on JBoss 4 from NetBeans IDE 4.1

So now that I can start JBoss and stop JBoss from the IDE, generate JBoss deployment descriptors from the IDE, deploy an application from the IDE to JBoss, and use the NetBeans IDE debugger to connect to JBoss and debug my application (as explained in previous blog entries), there's nothing more I can do to integrate JBoss with NetBeans IDE 4.1. Right? Wrong. There's also the cool HTTP Monitor which, according to the final section of "Chapter 6: Web Applications" in the NetBeans IDE Field Guide - Draft Chapters, is cool for the following reasons:

NetBeans IDE provides a built-in HTTP Monitor to help isolate problems with data flow
from JSP and servlet execution on a Web server. There is no need to add logic to your Web
application to trace HTTP requests and associated state information. The NetBeans IDE built-in
HTTP Monitor can do this for you.

When the IDE is configured with a Web container or a Web application is deployed with a
NetBeans HTTP Monitor servlet filter and filter mapping, the HTTP Monitor will automatically
record all HTTP requests made to the Web container. For each HTTP request that is processed
by the Web container, the HTTP Monitor not only records the request, it also records state
information maintained in the Web container.

By using the HTTP Monitor, you can analyze HTTP requests, and store HTTP GET and
HTTP POST requests for future analysis sessions. You can also edit these stored requests and
replay them. This is a powerful feature to help isolate data flow and state information passed
within an HTTP request to a Web container. HTTP requests are stored until you exit the IDE
unless you explicitly save them.
The HTTP Monitor is easily enabled if the server you're using is registered in NetBeans IDE 4.1. Only the Sun Java System Application Server and the Tomcat Web Server can be registered in IDE 4.1. JBoss can't. But does that mean that we can't use the HTTP Monitor with JBoss? No, it doesn't. The steps to set it up are reasonably simple (and are also described in the IDE's own help system):

  1. Right-click the project's Libraries node in the Projects window. Choose Add JAR/Folder and add the following JARs:

  2. Build the project. When you build a standard project, the JARs get moved to the build/web/WEB-INF/lib folder. This is very important to know because, when you build the project to JBoss's deployment directory, at the end of these instructions, you must include these JAR files. Knowing the exact location to which the IDE moves the JAR files allows you to create the appropriate Ant build script to copy them to JBoss.

  3. In the IDE, go to the web.xml file and double-click it. The cool (new in NetBeans IDE 4.1) web.xml Visual Editor opens in the Source Editor. Go to the Filters tab at the top of the editor, click Add Filter Element, and add the following values:

    • Filter Name: HTTPMonitorFilter
    • Filter Class: org.netbeans.modules.web.monitor.server.MonitorFilter

    Click OK, click on Add in the "Initialization Parameters" subsection, and add the following values:

    • Param Name: netbeans.monitor.ide
    • Param Value: name-of-host-that-runs-IDE:port-of-internal-HTTP-server

    To find out the port of the internal HTTP Server, go to the Runtime window, right-click the HTTP Server node, choose Properties, et voila, there it is. For me, it is set to 8086 (probably the default, at least for Windows).

    Click OK, click on the Filter Mappings header below, click Add, and add the following values:

    • Filter Name: HTTPMonitorFilter
    • URL Pattern: /\*
    • Dispatcher Types: REQUEST, FORWARD, INCLUDE, ERROR

    Click OK. You should now have the following in the web.xml Visual Editor:

    And when you click XML at the top of the editor, the following will be included in the web.xml file:

  4. Go to the Runtime window, right-click the HTTP Server, and click Start Server.

  5. Now build and deploy your application to JBoss. Remember that you need the JAR files that you picked up in step 1 to be included with the resources that you've copied to JBoss's deployment directory! When you run the application, the HTTP Monitor will start up in the IDE:

    Now you can do any and all of the following:

    • Analyze HTTP requests records.
    • Save HTTP request records.
    • Edit HTTP request records.
    • Refresh request records.
    • Sort HTTP request records.
    • Delete HTTP request records.
    • Replay HTTP request records.

    Two very important notes:

    • Whenever you restart the IDE, you should also restart the HTTP Server, if you want to use the HTTP Monitor with JBoss (or any other server other than the Tomcat Web Server). What I did, a few times, is exit the IDE, restart the IDE, build and deploy to JBoss, and then wonder: "Huh? Where's the HTTP Monitor?" The reason why it didn't appear was because I hadn't restarted the HTTP Server...
    • Since the HTTP Monitor is used to debug in the pre-production phase of your development cycle, remember that when you deploy the application to a production server, you should remove the JAR files from your project and remove the filter and filter mapping declarations from the web.xml file.

A good application to test all of this out on is the "Midnight Cookie" application, which is what you end up with at the end of the NetBeans IDE 4.1 Tutorial for Web Applications. In fact, the second part of that tutorial provides extensive information, including many screenshots, on the various things you can do with the HTTP Monitor.

Develop in NetBeans IDE 4.1, Deploy Anywhere

An unnamed developer, in an as-yet unpublished article, which I was lucky enough to read, really clarified a couple of things for me today. In a recent blog entry, it suddenly dawned on me that even though I had some cool little Ant scripts (hooked up to cool little NetBeans IDE menu items) for starting, debugging, stopping, testing, and deploying to a variety of servers (JBoss 4, Tomcat 4, and JRun 4), I couldn't really develop much in the IDE for these servers, because each has its own server-specific deployment descriptors (such as sun-web.xml, jboss-web.xml, jboss.xml, config.xml). At the end of that blog entry I had a brief Eureka moment, when I blogged the fact that I had just discovered the Migration Tool for the Sun Java System Application Server 8 Download.

However, thanks to this unnamed developer (unnamed because his article hasn't been officially released yet) my Eureka moment is even bigger. I hope I'm not stealing his thunder, but his article mentions the absolutely brilliant XDoclet, which can transform deployment descriptors from one server vendor into the deployment descriptors of another. I guess to many people that's not really news, because XDoclet's been around for a while. But, for me, the following is pretty revolutionary:

    <target name="Transform-JBoss-Web" depends="init,init-xdoclet" description="Generate web deployment descriptors">
       <webdoclet destdir="${build.dir.jboss}/WEB-INF" 
                addedtags="@xdoclet-generated at ${TODAY},@copyright The XDoclet Team,@author XDoclet" 
            <fileset dir="${src.root}">
                <include name="\*\*/\*Servlet.java"/>

            <jbosswebxml version="4.0" />

Anyway, that's all I'm going to say, because otherwise the unnamed developer's article is not going to be as cool and educational as I suspect it will be.

Update 04/20/05: Click here to read the cool article referred to above.


Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« April 2005 »