Вы находитесь на странице: 1из 4

Powerful browser detection and custom JSP tags with Geronimo Page 1 of 4

Powerful browser detection and custom JSP


tags with Geronimo
Use Geronimo and Jetty to create a browser and OS detection scheme

Daniel Wintschel (daniel@humandoing.net), Author and developer, 自由职业者

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.

Date: 13 Sep 2005


Level: Intermediate
PDF: A4 and Letter (341 KB | 26 pages)Get Adobe® Reader®

Activity: 483 views


Comments: 0 (Add comments)

Average rating (based on 0 votes)

JSP components and sample usage

This section briefly outlines the JSP components within the application and provides two examples of
sample usage of the custom tags you've developed.

Detection JSP -- JavaScript

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.

Listing 11. The detect.jsp page


<%
String originallyRequestedPage =
(String) request.getAttribute( "RequestURI");
%>
<html>
<head>
<title>Detecting Browser Information...</title>
</head>
<body>
<form name="detect" method="post" action="BrowserDetect">
<input type="hidden" name="brow" value="" />
<input type="hidden" name="moz_brow" value="" />
<input type="hidden" name="nu" value="" />
... // more hidden form field...
... // omitted for the sake of brevity...

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>

<script type="text/javascript" language="JavaScript">

<%--
Include our open source browser detection javascript

Credit given to Harald Hope and Tapio Markula

JavaScript from below is taken from the file browser_detection.js


which is included in this project and is released under the GNU LGPL.

http://techpatterns.com/
http://techpatterns.com/downloads/javascript_browser_detection.php

Lesser GPL license text:


http://www.gnu.org/licenses/lgpl.txt
--%>
//initialization, browserInfo, os detection
var d, dom, nu='', brow='', ie, ie4, ie5, ie5x, ie6, ie7;
... // lots more variable declarations...
... // omitted for the sake of brevity...

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;

... // lots more assignments that assign values to


... // hidden form field elements...
... // omitted for the sake of brevity...
document.detect.submit();
//-->
</script>

</body>
</html>

Listing 11 contains HTML, JavaScript, and Java snippets that can be found in detect.jsp. The page
executes line by line and:

1. Prints out an HTML form with hidden form fields.

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

2. Declares JavaScript variables.


3. Uses the functions and abilities given by JavaScript to assign values to the declared variables
that are indicative of the browser name, browser version, and operating system with which the
user is making this request.
4. Assigns the detected values to the hidden form field elements.
5. Submits the HTML form back to the server automatically.

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.

Listing 12. The index.jsp page

<%@ taglib uri="/WEB-INF/lib/browserdetection.jar" prefix="bd" %>


<html>
<head>
<title>Browser Detection Test</title>
</head>
<body>
This is a basic server-side check of the browser using session-persisted values
that were previously determined from an open-source JavaScript.

<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>

Let's see what else:

<a href="index2.jsp">Check OS</a>

</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.

Listing 13. The index2.jsp page

<%@ taglib uri="/WEB-INF/lib/browserdetection.jar" prefix="bd" %>


<html>
<head>
<title>Browser Detection Test</title>
</head>
<body>
This is a server-side check of the operating system using session-persisted values
that were previously determined from an open-source JavaScript.

<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

Trademarks | My developerWorks terms and conditions

http://www.ibm.com/developerworks/opensource/tutorials/os-ag-browserjsp/section6.html 9/19/2010

Вам также может понравиться