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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/968800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:43:42  来源:igfitidea点击:

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

javajspbrowsercross-browseruser-agent

提问by Christopher Tokar

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.

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

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?

使用 JSTL、EL 等而不是 scriptlet 执行此操作的最简洁方法是什么?

采纳答案by laginimaineb

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

回答by Zoltan

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

回答by mahesh nanayakkara

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

如果您使用的是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>