Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用jsp对request.getHeader中的值进行测试?_Jsp_Jstl_El_User Agent - Fatal编程技术网

如何使用jsp对request.getHeader中的值进行测试?

如何使用jsp对request.getHeader中的值进行测试?,jsp,jstl,el,user-agent,Jsp,Jstl,El,User Agent,我正在设置一个变量: <% String userAgent = request.getHeader("user-agent");%> 如何使用userAgent测试不同的场景 <c:choose></c:choose> 我不知道如何设置它?您不想将scriptlet与taglibs/EL混合使用。它们不在同一范围内运行 隐式映射对象${header}可以使用EL中的所有请求头。由于用户代理标题名包含一个特殊字符-,因此${header.user

我正在设置一个变量:

<% String userAgent = request.getHeader("user-agent");%>

如何使用userAgent测试不同的场景

<c:choose></c:choose>

我不知道如何设置它?

您不想将scriptlet与taglibs/EL混合使用。它们不在同一范围内运行

隐式映射对象
${header}
可以使用EL中的所有请求头。由于
用户代理
标题名包含一个特殊字符
-
,因此
${header.user agent}
无法按预期工作,因此需要使用大括号符号
[]
对其进行引用

${header['user-agent']}
因此,这应该做到:


你在假装使用基于壁虎的浏览器。
你在假装使用基于MSIE的浏览器。
你在假装使用基于Webkit的浏览器。
不清楚你在假装使用哪个浏览器。

与问题无关:以这种方式检查用户代理头是一种代码味道。用户代理头完全由客户端控制,很容易被欺骗成一个完全不同的值(这就是为什么我在上面的代码示例中使用术语“假装”)。如何正确地解决真正的功能需求,您想对真正的功能需求做更多的阐述

例如,在JavaScript中,您应该更喜欢浏览器检测

或者,当您希望根据媒体类型加载特定的样式表时,您应该使用。例如



或者,当您希望向最终用户提供用户代理标头中信息的用户友好摘要时,您希望使用单独的服务,例如。它们还提供了一种气味。

你所说的代码气味是什么意思?这似乎是将iphone/android作为用户代理进行检测的一个好方法,因此可以针对这些移动平台注入样式表。你能建议一些不同的东西吗?谢谢-这很有道理。我将首先尝试样式表方法。我更新了答案,示例中的
smartphone.css
应该是iPhone/Android所需要的。值得注意的是,EL和Scriptlet不在同一范围内运行!
<link rel="stylesheet" href="screen.css" media="screen,projection,tv" />
<link rel="stylesheet" href="smartphone.css" media="only screen and (max-device-width:480px)"/>
<link rel="stylesheet" href="handheld.css" media="handheld" />
<link rel="stylesheet" href="print.css" media="print" />