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

发布于 2024-11-25 10:12:18 字数 218 浏览 1 评论 0原文

我设置了一个变量:

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

来针对 userAgent 测试不同的场景?

<c:choose></c:choose>

如何使用我不知道如何设置它

Im setting up a variable:

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

How do I test different scenarios against userAgent using

<c:choose></c:choose>

Im not sure how to set it up?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

镜花水月 2024-12-02 10:12:18

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

所有请求标头均位于 EL 中,可通过隐式映射对象 ${header} 获取。由于 user-agent 标头名称包含特殊字符 -,因此 ${header.user-agent} 无法按预期工作,您需要使用大括号 [] 来引用它。

${header['user-agent']}

因此,这应该是:

<c:choose>
    <c:when test="${fn:contains(header['user-agent'], 'Gecko')}">
        You're pretending to use a Gecko based browser.
    </c:when>
    <c:when test="${fn:contains(header['user-agent'], 'MSIE')}">
        You're pretending to use a MSIE based browser.
    </c:when>
    <c:when test="${fn:contains(header['user-agent'], 'Webkit')}">
        You're pretending to use a Webkit based browser.
    </c:when>
    <c:otherwise>
        It's not clear which browser you're pretending to use.
    </c:otherwise>
</c:choose>

与问题无关:以这种方式检查用户代理标头是一种代码味道。用户代理标头完全由客户端控制,并且可以轻松地被欺骗成完全不同的值(这就是我在上面的代码示例中使用术语“假装”的原因)。如何正确解决真实的功能需求,你想详细说明一下真实的功能需求。

例如,在 JavaScript 中,您应该更喜欢功能检测而不是浏览器检测。

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

<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" />

,或者当您想向最终用户提供用户代理标头中找到的信息的用户友好摘要时,您希望使用单独的服务,例如 http://user-agent-string.info/。他们还提供了 Java API 示例

You don't want to mix scriptlets with taglibs/EL. They do not run in the same scope.

All request headers are in EL available by an implicit mapped object ${header}. Since the user-agent header name contains a special character - so that ${header.user-agent} don't work as expected, you need to use the brace notation [] which quotes it.

${header['user-agent']}

So, this should do:

<c:choose>
    <c:when test="${fn:contains(header['user-agent'], 'Gecko')}">
        You're pretending to use a Gecko based browser.
    </c:when>
    <c:when test="${fn:contains(header['user-agent'], 'MSIE')}">
        You're pretending to use a MSIE based browser.
    </c:when>
    <c:when test="${fn:contains(header['user-agent'], 'Webkit')}">
        You're pretending to use a Webkit based browser.
    </c:when>
    <c:otherwise>
        It's not clear which browser you're pretending to use.
    </c:otherwise>
</c:choose>

Unrelated to the problem: checking the user agent header this way is a code smell. The user agent header is fully controlled by the client and can easily be spoofed into a completely different value (that's why I used the term "pretending" in the above code example). How to solve the real functional requirement properly, you'd like to elaborate a bit more about the real functional requirement.

For example, in JavaScript you should prefer feature detection over browser detection.

Or when you'd like to load specific stylesheets based on the media type, you should rather use the media attribute. E.g.

<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" />

Or when you'd like to present the enduser an user-friendly summary of the information found in the user agent header, you'd like to use a separate service instead such as http://user-agent-string.info/. They also offer a Java API example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文