Christopher Tokar
Christopher Tokar

Reputation: 11917

How do you check the browser's user agent in a JSP page using JSTL, EL?

I need to check the browser's user-agent to see if it is IE6. However I shouldn't use scriptlets (we have a strict no scriptlets policy) to do this.

Currently I use

<%
String ua = request.getHeader( "User-Agent" );
boolean isMSIE = ( ua != null && ua.indexOf( "MSIE" ) != -1 );
%>

<% if( isMSIE ){ %>
<div>
<% } %>

What is the cleanest way to do this using JSTL, EL, etc and not scriptlets?

Upvotes: 18

Views: 39709

Answers (3)

mahesh nanayakkara
mahesh nanayakkara

Reputation: 616

If you are using spring-mobile framework you can use following to check device type

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
    <c:choose> 
        <c:when test="${currentDevice.normal}"><p>"Welcome desktop user"</p> </c:when>
        <c:when test="${currentDevice.mobile}"><p>"Welcome mobile user"</p>  </c:when>
        <c:when test="${currentDevice.tab}"><p>"Welcome tab user"</p> </c:when>
    </c:choose>

Upvotes: 2

Zoltan
Zoltan

Reputation: 211

<c:if test="${fn:contains(header['User-Agent'],'MSIE')}"></c:if>

Upvotes: 21

laginimaineb
laginimaineb

Reputation: 8295

<c:set var="browser" value="${header['User-Agent']}" scope="session"/>

Upvotes: 25

Related Questions