如何访问由 JSP 中的 servlet 设置的请求属性? [英] How to access a request attribute set by a servlet in JSP?

查看:24
本文介绍了如何访问由 JSP 中的 servlet 设置的请求属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 JSP 页面中检索由 servlet 设置的属性值,但我只对 ${param} 的参数感到幸运.我不确定我能做些什么不同.也许它很简单,但我还做不到.

I'm trying to retrieve attribute values set by a servlet in a JSP page, but I've only luck with parameters by ${param}. I'm not sure about what can I do different. Maybe its simple, but I couldn't manage it yet.

public void execute(HttpServletRequest request, HttpServletResponse response) {

    //there's no "setParameter" method for the "request" object
    request.setAttribute("attrib", "attribValue");

    RequestDispatcher rd = request.getRequestDispatcher("/Test.jsp");
    rd.forward(request,response);
}

在 JSP 中,我一直试图检索attribValue",但没有成功:

In the JSP I have been trying to retrieve the "attribValue", but without success:

<body>
    <!-- Is there another tag instead of "param"??? -->
    <p>Test attribute value: ${param.attrib}
</body>

如果我在整个过程(调用页面、servlets 和目标页面)中传递一个参数,它的效果非常好.

If I pass a parameter through all the process (invoking page, servlets and destination page), it works quite good.

推荐答案

它已经在默认的 EL 范围内可用,所以只需

It's available in the default EL scope already, so just

${attrib}

应该这样做.

如果你想明确指定scope(EL会依次搜索page、request、session和application scope中第一个匹配属性名的非空属性值),那么你需要通过scope来引用而是映射,这是请求范围的 ${requestScope}

If you like to explicitly specify the scope (EL will namely search the page, request, session and application scopes in sequence for the first non-null attribute value matching the attribute name), then you need to refer it by the scope map instead, which is ${requestScope} for the request scope

${requestScope.attrib}

这仅在您可能在页面范围内具有完全相同名称的属性时才有用,否则将获得优先级(但这种情况通常表明设计很差).

This is only useful if you have possibly an attribute with exactly the same name in the page scope which would otherwise get precedence (but such case usually indicate poor design after all).

这篇关于如何访问由 JSP 中的 servlet 设置的请求属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆