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

我的未来我决定 提交于 2019-11-28 02:38:09

问题


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?


回答1:


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



回答2:


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



回答3:


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>


来源:https://stackoverflow.com/questions/968800/how-do-you-check-the-browsers-user-agent-in-a-jsp-page-using-jstl-el

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!