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

查看:135
本文介绍了如何在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>

如果我通过一个参数传递所有进程(调用页面,servlet和目标页面),它的工作原理非常好。

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}

应该这样做。

如果您想明确指定范围(EL将按顺序搜索与属性匹配的第一个非null属性值的页面,请求,会话和应用程序范围name),然后你需要通过范围映射来引用它,对于请求范围是 $ {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).

  • Our EL wiki page
  • Java EE 6 tutorial - Expression Language

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

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