什么是隐含对象?这是什么意思? [英] What are implicit objects? What does it mean?

查看:232
本文介绍了什么是隐含对象?这是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我研究JSP和Servlets时,我都会遇到单词隐含对象,这个术语是什么意思?

Whenever I study JSP and Servlets I come across word implicit objects, what does the term mean?

如何调用它们程序没有实例化对象?谁实例化隐式对象?请详细说明。

How they are called in my program without instantiating objects? Who instantiates implicit objects? Please elaborate.

谢谢

推荐答案

这些已经是对象了已被servlet容器放置在范围内,以便EL(表达式语言)可以访问它,例如 PageContext HttpServletRequest#getParameter() HttpServletRequest#getHeader() 等等。这些只是为了方便,因此您不需要使用旧的 scriptlet 来抓取它们。

Those are objects which are already been placed in the scope by the servlet container, so that it's accessible by EL (Expression Language), such as the PageContext, HttpServletRequest#getParameter(), HttpServletRequest#getHeader() and so on. Those are just for convenience so that you don't need to use old-fahioned scriptlets to grab them.

因此,而不是例如

<%= pageContext.getSession().getMaxInactiveInterval() %><br>
<%= request.getParameter("foo") %><br>
<%= request.getHeader("user-agent") %><br>
<%  for (Cookie cookie : request.getCookies()) { // Watch out with NPE!
        if (cookie.getName().equals("foo")) {
            out.write(cookie.getValue());
        }
    }
%><br>

你可以这样做

${pageContext.session.maxInactiveInterval}<br>
${param.foo}<br>
${header['user-agent']}<br>
${cookie.foo}<br>

您会看到它们遵循要访问的JavaBean约定(即您只需调用JavaBean的getter)办法)。您看到我使用括号符号 [] 来获取用户代理,这是因为 - 是EL中的保留字符,因此 $ {header.user-agent} 不会起作用,它会尝试返回 request.getHeader(user) - pageContext.findAttribute(agent)的结果,这显然不起作用。

You see that they follows the JavaBean conventions to be accessed (i.e. you can just invoke the getters the JavaBean way). You see that I used the brace notation [] to get the user-agent, that's because the - is a reserved character in EL, so ${header.user-agent} isn't going to work, it would try to return the result of request.getHeader("user") - pageContext.findAttribute("agent") which is obviously not going to work.

有关它们的概述,请查看 隐式对象

For an overview of them all, check the chapter Implicit Objects in the Java EE tutorial.

这篇关于什么是隐含对象?这是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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