Get user agent with Server Side Includes

This is how you can show the user agent with Server Side Includes:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta charset="utf-8" />
    <title>SSI Page</title>
  </head>
  <body>
    <!--#echo var="HTTP_USER_AGENT" -->
  </body>   
</html>

IndexOutOfBoundsException with Catalina SSI Servlet

I played around with some Server Side Includes using org.apache.catalina.ssi.SSIServlet and noticed that some statements (which were fine on Apache) are not working on GlassFish. Finally I found the solution.

In Apache (using mod_ssi) you can do this:

<!--#if expr="{$HTTP_USER_AGENT} == /(iPhone)/" -->
<p>It's an iPhone.</p>
<!--#endif -->

But on GlassFish Server (with SSIServlet) you have to do this:

<!--#if expr="{$HTTP_USER_AGENT} = '/(iPad)/'" -->
<p>It's an iPhone.</p>
<!--#endif -->

How to use Server Side Includes

Server Side Includes (SSI) is a server-side scripting language which runs on Apache (with mod_ssi), some Java Application Servers (see Server Side Include in GlassFish) and also on Microsoft Internet Information Services (IIS). Here are a few examples of how to use SSI:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Server Side Includes</title>
    <style type="text/css">
      * { margin: 0; padding: 0; border: 0; }
    </style>
  </head>
  <body>  
    <!-- http://httpd.apache.org/docs/2.0/howto/ssi.html -->
 
    <!--#set var="name" value="Benny Neugebauer" --> 
    <!--#set var="text" value="My name is ${name}." --> 
    <!--#set var="accountBalance" value="\$72.72" --> 
 
    <p>
      <!--#echo var="name" --><br/>
      <!--#echo var="text" --><br/>
      My account balance is: <!--#echo var="accountBalance" -->
      <br/><br/>
      <!--#if expr="${name} == 'Benny Neugebauer'" -->
      <b>Hey Benny!</b>
      <!--#else -->
      <em>Who are you??</em>
      <!--#endif --> 
      <br/>
      <!--#if expr="{$name} = /[a-zA-Z]/" -->
      Your name contains just letters. Yay!
      <!--#endif -->
      <br/><br/>
    </p>
 
    <p>
      <!-- Print some CGI variables... -->
      <!--#echo var="HTTP_USER_AGENT" --><br/>
      <!--#echo var="SERVER_NAME" --><br/>
      <!--#echo var="REMOTE_ADDR" --><br/>
    </p>
  </body>
</html>