O'Reilly logo

Java and XSLT by Eric M. Burke

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Identifying the Browser

A strength of XSLT is its ability to help keep data and presentation separate. As you know, supporting different transformations is a matter of writing different XSLT stylesheets. Figuring out which stylesheet to apply is the only missing piece. For web applications, the User-Agent HTTP header offers the solution.

HTTP requests consist of a header followed by content; the header contains name/value pairs of data, allowing the client and server to exchange additional information with each other. The text shown in Example 8-7 contains the complete HTTP request issued by Netscape 6.0 when running on Windows 2000.

Example 8-7. Netscape 6 HTTP request

GET / HTTP/1.1
Host: localhost:80
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; m18) Gecko/20001108 
Netscape6/6.0
Accept: */*
Accept-Language: en
Accept-Encoding: gzip,deflate,compress,identity
Keep-Alive: 300
Connection: keep-alive

For the purposes of browser detection, the value of User-Agent must be parsed to determine what kind of browser is requesting information from the servlet. Based on this information, the servlet can select an appropriate XSLT stylesheet that supports the particular strengths and weaknesses of the browser in question.

Unfortunately, there are hundreds of variations of User-Agent, and browser vendors do not rigorously adhere to any standard format. The common browsers can be identified, however, with a small amount of parsing logic. Table 8-2 lists some of the more common ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required