JSTL Agent identification

Posted by Phil on October 30, 2008 under Web Development, Websphere Commerce | 3 Comments to Read

jstl-agent-identification

So converting NT2.2 to be IE6 friendly was a kick in the teeth… and as Dustin posted earlier, it would have been nice to throw up a warning window and let them see the horror or an IE6 rendering job.  However we coded it up to be usable in IE6 and in the process had to rewrite some of the homepage.  At first I used the ugly <![if IE 6]> // do something magical <![endif]–> tags, but they caused Safari to explode on itself.  So I went back did a quick study and came out with a JSTL solution, and then modded it with Drew to get it working.

Using JSTL you can get the browser through the user-agent string in the URL request header. Like this:

?View Code HTML4STRICT
<c:set var="browser" value="${header['User-Agent']}" scope="page"/>

You can see that I used a c:set to get the ‘User-Agent’ string and set the variable ‘browser’ to that value.

Now we just need to set up a c:choose to determine the path of IE 6 or other browser types using the indexOf function in the JSTL ‘fn’ library.  So a quick c:choose statement would look like this.

?View Code HTML4STRICT
<c:set var="browser" value="${header['User-Agent']}" scope="page"/>
<c:choose>
	<c:when test="${!empty browser && fn:indexOf(browser, 'MSIE 6') >= 0}">
		NOT EMPTY
	</c:when>
	<c:otherwise>
		EMPTY
	</c:otherwise>
</c:choose>

In the ‘c:when test’ you can see that the indexOf ends with a ‘>=0′, this is because indexOf returns an INT, which is treated Boolean-esque in this case (it’s not a Boolean, but the way we are using it resembles a Boolean).

Don’t forget that when using the JSTL fn and c tag libraries you need to include them in your jsp file.  use the following at the top of your page to use these excellent tag-libs.

?View Code HTML4STRICT
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

If you have any questions, leave’em in the comments :) .

  • Dustin said,

    This is also possible with JavaScript, but we all know how Drew loves JavaScript.

  • Dustin said,

    getting the user agent I mean.

  • Phil said,

    Absolutely, and this is a nice server-side way of dealing with it. I likes me the power of js though… :)

Add A Comment