代码之家  ›  专栏  ›  技术社区  ›  Christopher Tokar

如何使用JSTL、EL在JSP页面中检查浏览器的用户代理?

  •  16
  • Christopher Tokar  · 技术社区  · 15 年前

    我需要检查浏览器的用户代理是否是IE6。但是,我不应该使用scriptlets(我们有严格的无scriptlets策略)来执行此操作。

    目前我使用

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

    使用JSTL、EL等而不是Scriptlet,最干净的方法是什么?

    3 回复  |  直到 9 年前
        1
  •  24
  •   laginimaineb    15 年前
    <c:set var="browser" value="${header['User-Agent']}" scope="session"/>
    
        2
  •  20
  •   Welbog    15 年前
    <c:if test="${fn:contains(header['User-Agent'],'MSIE')}"></c:if>
    
        3
  •  1
  •   mahesh nanayakkara    9 年前

    如果您正在使用 spring-mobile 可使用以下框架检查设备类型

    <%@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>