Powerful Browser Detection and Custom JSP Tags with Geronimo Use Geronimo and Jetty to Create a Browser and OS Detection Scheme
Total Page:16
File Type:pdf, Size:1020Kb
Powerful browser detection and custom JSP tags with Geronimo Use Geronimo and Jetty to create a browser and OS detection scheme Skill Level: Intermediate Daniel Wintschel ([email protected]) Author and developer Freelance 13 Sep 2005 Learn how to take advantage of Apache Geronimo and Jetty to create a powerful browser and open source detection scheme. In addition to the open source (OS) Geronimo application server, this tutorial shows you how to use freely available JavaScript programs to perform browser and OS detection on the client side. Section 1. 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. About this tutorial Powerful browser detection and custom JSP tags with Geronimo © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 1 of 26 developerWorks® ibm.com/developerWorks 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. Prerequisites Powerful browser detection and custom JSP tags with Geronimo Page 2 of 26 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks® 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. System requirements To run the example code in this tutorial, you need to perform the following steps: 1. Download and install the following applications: • Geronimo Milestone 4 or later. • Apache ANT 1.6.5. • J2SE 1.4.2_09. 2. 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_HOME Set to the root folder of your Geronimo installation ANT_HOME Set to the root folder of your Ant installation JAVA_HOME Set to the root folder of your Java installation PROJECT_HOME Set to the root folder of your BrowserDetection application PATH Ensure that ANT_HOME/bin is in your PATH 3. 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. Powerful browser detection and custom JSP tags with Geronimo © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 3 of 26 developerWorks® ibm.com/developerWorks Section 2. Custom JSP tags -- a refresher This section provides an overview or refresher on what a custom JSP tag is and why you might want to develop your own JSP tags. Tag libraries: What and why? Tag libraries and custom JSP tags were introduced into the JSP specification at Version 1.1. In technical terms, the purpose of a custom tag is to create and access programming objects that are used to affect the output stream that is being sent back to the user's browser. Custom tags can also help break your JSP pages down into small components, allowing easier customization of the presentation layer. Some other examples of tasks that a custom tag may perform include flow control, form processing, accessing databases, and sending e-mail. The tag we'll look at relates closest to a tag that performs some type of flow control, displaying one set of data on a browser and another set of data on a different browser. Geronimo fits into this picture as the J2EE server that runs Jetty. Jetty is the servlet container that performs the execution of the custom tags you write. Tag libraries: How? Here's a brief description of the steps involved in creating and deploying a tag library within an application. At the most basic level, you need to write a Java class that extends javax.servlet.jsp.tagext.TagSupport or javax.servlet.jsp.tagext.BodyTagSupport. These classes are part of the standard servlet API and come bundled with Geronimo. Following this, write a Tag Library Definition (TLD) file that describes to the servlet container the name of your custom tags, what attributes they have, and whether those attributes are mandatory. After you've created the necessary Java classes and TLD files, you're ready to bundle the tag with your application and deploy it to your application server. In this application, the class files and TLD files that make up your tags are bundled within a .jar (Java ARchive) file. The .jar file is deployed with your application and contained inside the application .war (Web ARchive) file. Although this is a high-level overview of the process of developing custom tags, later in the tutorial you'll see specific examples outlining Java classes that make up a custom JSP tag. You'll also see the accompanying TLD file and the structure of the Web application that you'll deploy to Geronimo. Powerful browser detection and custom JSP tags with Geronimo Page 4 of 26 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks® Section 3. JavaScript for browser detection This section describes why JavaScript was chosen for browser detection and outlines other potential uses for JavaScript-based detection mechanisms. Why JavaScript? So why use JavaScript for browser detection instead of the user-agent string that the browser sends to the server with each request? In JavaScript you can detect many additional things that could be useful on the server side, including screen height and width, browser plug-ins that are installed (for example, QuickTime, Flash, Shockwave, and Java Plug-in), and the versions of all the plug-ins. This information can be beneficial (and sometimes necessary) to have access to the server side when preparing to deliver content to a user. The role of JavaScript in the application Within the browser detection application, JavaScript plays a simple but important role. The application includes a JSP with some embedded JavaScript that analyzes data about the user's browser and operating system. You'll have the JavaScript assign these values to hidden HTML form fields and submit the form to the server. The following sections describe the server-side components and how the client-side and server-side pieces fit together. Section 4. Design strategy This section describes the overall design strategy for the application. Learn how the application is designed, and learn about the build structure of the application to give you a better idea of how the components work together.