5.5 Differentiating Among Different Browser Types

The User -Agent header identifies the specific browser that is making the request. Although use of this header appears straightforward at first glance, a few subtleties are involved:

  • Use User-Agent only when necessary. Otherwise, you will have difficult-to-maintain code that consists of tables of browser versions and associated capabilities. For example, instead of remembering that the Windows version of Internet Explorer 5 supports gzip compression but the MacOS version doesn't, check the Accept-Encoding header. Instead of remembering which browsers support Java and which don't, use the APPLET tag with fallback code between <APPLET> and </APPLET> .

  • Check for null . Sure, all major browser versions send the User-Agent header. But, the header is not required by the HTTP 1.1 specification, some browsers let you disable it (e.g., Opera), and custom clients (e.g., Web spiders or link verifiers) might not use the header at all. In fact, you should always check that the result of request.getHeader is non- null before trying to use it, regardless of which header you are dealing with.

  • To differentiate between Netscape and Internet Explorer, check for "MSIE," not "Mozilla." Both Netscape and Internet Explorer say "Mozilla" at the beginning of the header, even though Mozilla is the Godzilla-like Netscape mascot. This characteristic is for compatibility with JavaScript.

  • Note that the header can be faked. Some browsers let the user change the value of this header. Even if the browser didn't allow this, the user could always use a custom client. If a client fakes this header, the servlet cannot tell the difference.

Listing 5.4 shows a servlet that sends browser-specific insults to users. For the sake of simplicity, it assumes that Internet Explorer and Netscape are the only two browsers being used. Specifically, it assumes that any browser whose User-Agent contains "MSIE" is Internet Explorer and any whose User-Agent does not is Netscape. Figures 5-4 and 5-5 show the results.

Listing 5.4 BrowserInsult.java
 package coreservlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** Servlet that gives browser-specific insult.  *  Illustrates how to use the User-Agent  *  header to tell browsers apart.  */ public class BrowserInsult extends HttpServlet {   public void doGet(HttpServletRequest request,                     HttpServletResponse response)       throws ServletException, IOException {     response.setContentType("text/html");     PrintWriter out = response.getWriter();     String title, message;     // Assume for simplicity that Netscape and IE are     // the only two browsers.  String userAgent = request.getHeader("User-Agent");   if ((userAgent != null) &&   (userAgent.indexOf("MSIE") != -1))  {       title = "Microsoft Minion";       message = "Welcome, O spineless slave to the " +                 "mighty empire.";     } else {       title = "Hopeless Netscape Rebel";       message = "Enjoy it while you can. " +                 "You <I>will</I> be assimilated!";     }     String docType =       "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +       "Transitional//EN\">\n";     out.println(docType +                 "<HTML>\n" +                 "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +                 "<BODY BGCOLOR=\"#FDF5E6\">\n" +                 "<H1 ALIGN=CENTER>" + title + "</H1>\n" +                 message + "\n" +                 "</BODY></HTML>");   } } 
Figure 5-4. The BrowserInsult servlet as viewed by a Netscape user.

graphics/05fig04.jpg

Figure 5-5. The BrowserInsult servlet as viewed by an Internet Explorer user.

graphics/05fig05.jpg



Core Servlets and JavaServer Pages (Vol. 1.Core Technologies)
Core Servlets and Javaserver Pages: Core Technologies, Vol. 1 (2nd Edition)
ISBN: 0130092290
EAN: 2147483647
Year: 2002
Pages: 194

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net