Before you start
This tutorial is for developers who would like to implement some form of a browser detection API into an application. You can take advantage of Geronimo as the Java™ 2 Platform, Enterprise Edition (J2EE) application server (specifically its use of Jetty as a servlet container) as well as some client-side JavaScript. You'll be developing a few separate Java-based components including a servlet filter and two custom JavaServer Pages (JSP) tags. You'll use Apache Ant as a build tool to build the .war file that you'll deploy to Geronimo, so some familiarity with Ant is handy. You should be familiar with the concepts of filters and JSP tag libraries, and feel comfortable reading Java source code.
By the end of this tutorial, you should have an idea of how to develop your own basic servlet filters and JSP tag libraries, and be able to implement a robust browser detection API into a J2EE application with the Apache Geronimo application server.
The content in this tutorial may beg the question, "Why another browser detection tool?" or "Why not use something like BrowserHawk or one of the other available products on the market?" Sometimes it is better to reinvent the wheel when a product doesn't meet your needs or may not integrate cleanly or easily into your application. In addition, many tools, including BrowserHawk, are proprietary, which is not necessarily a bad thing; however, some developers prefer to use more open tools so they can easily adapt them to their specific needs. Geronimo is an excellent example of this. The Apache Software Foundation wanted to provide a completely open source J2EE application server to fulfill a need it felt was not being met within the Java developer community. Its reasoning was not that there weren't any good J2EE application servers around; it was because Apache wanted to build up an open source, certified J2EE server that could be supported by the Java developer community.
The techniques used in this tutorial are relevant not only to browser and OS detection; the same techniques can be used for the detection of which version of Macromedia Flash a client may have installed or which country they're located in. There are some terrific open source JavaScript programs that provide access to this information, and the same technique we're applying today with browser and OS detection can be applied using these other scripts as well.
This tutorial is structured as follows:
- Custom JSP tags -- a refresher provides a brief overview or refresher course about what custom JSP tags and tag libraries are and how to develop them.
- JavaScript for browser detection outlines the reasons behind choosing JavaScript for browser detection and the role it plays within our application.
- Design strategy gives you an overall understanding of how to design this sample application and of the the final build structure that you'll deploy to Geronimo.
- Server-side components details all of the server-side Java components and source code within the sample application and ties together how these components interact with each other.
- JSP components and sample usage details the JSP components used within the application and provides examples of how to use the custom tags developed in this application.
- Trying it out allows you to compile and build the sample application from source and provides all the commands necessary to deploy the application to Geronimo and test the functionality of the application for yourself.
This tutorial assumes that you have basic knowledge of the Java Servlet API and the Java programming language. Some knowledge of JavaScript won't hurt either.
To run the example code in this tutorial, you need to perform the following steps:
- Download and install the following applications:
- Make sure that the environment variables outlined in Table 1 are defined in your shell.
Table 1. Setting the environmental variables Variable name Required setting GERONIMO_HOMESet to the root folder of your Geronimo installation ANT_HOMESet to the root folder of your Ant installation JAVA_HOMESet to the root folder of your Java installation PROJECT_HOMESet to the root folder of your BrowserDetection application PATHEnsure that ANT_HOME/binis in yourPATH - Extract the supplied .zip file to your preferred location (this will be the project root).
The project is laid out as follows:
Listing 1. Project layout/BrowserDetect/ /conf/ ' contains taglib definition /deploy/ ' created by ANT (will contain WAR file) /src/ ' contains Java source code /web/ ' contains JSP, JavaScript and web.xml /build.xml ' ANT build file
If you are eager to compile and run the application, jump to the Trying it out section.

