Академический Документы
Профессиональный Документы
Культура Документы
Summary: 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.
This section briefly outlines the JSP components within the application and provides two examples of
sample usage of the custom tags you've developed.
The only real JSP component to worry about is the detect.jsp page. The other JSP components are just
simple examples of how to use the BrowserTag. The detect.jsp page is the page that the
BrowserDetectFilter forwards to when there is no BrowserInfo object associated with the current
user's session.
http://www.ibm.com/developerworks/opensource/tutorials/os-ag-browserjsp/section6.html 9/19/2010
Powerful browser detection and custom JSP tags with Geronimo Page 2 of 4
</form>
<%--
Include our open source browser detection javascript
http://techpatterns.com/
http://techpatterns.com/downloads/javascript_browser_detection.php
d=document;
n=navigator;
nav=n.appVersion;
nan=n.appName;
nua=n.userAgent;
old=(nav.substring(0,1)<4);
mac=(nav.indexOf('Mac')!=-1);
win=( ( (nav.indexOf('Win')!=-1) ||
(nav.indexOf('NT')!=-1) ) && !mac)?true:false;
lin=(nua.indexOf('Linux')!=-1);
...// lots more variable assignments and browser testing...
... // omitted for the sake of brevity...
document.detect.requested_page.value =
'<%=originallyRequestedPage%>';
document.detect.brow.value = brow;
document.detect.moz_brow.value = moz_brow;
document.detect.nu.value = nu;
document.detect.moz_brow_nu_sub.value = moz_brow_nu_sub;
</body>
</html>
Listing 11 contains HTML, JavaScript, and Java snippets that can be found in detect.jsp. The page
executes line by line and:
http://www.ibm.com/developerworks/opensource/tutorials/os-ag-browserjsp/section6.html 9/19/2010
Powerful browser detection and custom JSP tags with Geronimo Page 3 of 4
The action of the HTML form is BrowserDetect, and given the application context, it translates into
"/browserdetection/BrowserDetect". This works in the example application because there are no
subdirectories within its context.
Sample usage
Listing 12 and Listing 13 show simple JSP pages that act as the entry point to the application and
examples of how to use the custom JSP tags created in this tutorial.
<br><br>
<bd:Browser>
<bd:BrowserDetect check="isIE">
Look Ma!!! I'm Internet Explorer... Woof!
</bd:BrowserDetect>
<bd:BrowserDetect check="isMozilla">
Look Ma!!! I'm some kinda Mozilla Browser... Yum!
</bd:BrowserDetect>
</bd:Browser>
<br><br>
</body>
</html>
Listing 12 is the entry page -- or index page -- of the application. This is a simple example of your
final BrowserTag in action. If the user's browser is Microsoft® Internet Explorer, then the application
prints Look Ma!!! I'm Internet Explorer... Woof!. If the browser is Mozilla, then Look Ma!!!
I'm some kinda Mozilla Browser... Yum! prints. It's worth noting again that the example
http://www.ibm.com/developerworks/opensource/tutorials/os-ag-browserjsp/section6.html 9/19/2010
Powerful browser detection and custom JSP tags with Geronimo Page 4 of 4
application does not provide an else function. For example, if the user is not using Internet Explorer
or Mozilla, then no output is displayed. To achieve this, you need to add a little more functionality to
the BrowserTag.
Listing 13 is similar to index.jsp, but demonstrates how to achieve operating system-specific content
as opposed to browser-specific content.
<br><br>
<bd:Browser>
<bd:BrowserDetect check="isWindows">
I'm running on Windows
</bd:BrowserDetect>
<bd:BrowserDetect check="isLinux">
Linux is us, and we're you're friend
</bd:BrowserDetect>
<bd:BrowserDetect check="isMac">
Mac - what else can we say? Yum!
</bd:BrowserDetect>
</bd:Browser>
</body>
</html>
6 of 11 | Previous | Next
http://www.ibm.com/developerworks/opensource/tutorials/os-ag-browserjsp/section6.html 9/19/2010