Java 什么是隐式对象?这是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2113286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are implicit objects? What does it mean?
提问by giri
Whenever I study JSP and Servlets I come across word implicit objects, what does the term mean?
每当我学习 JSP 和 Servlets 时,我都会遇到隐式对象这个词,这个词是什么意思?
How they are called in my program without instantiating objects? Who instantiates implicit objects? Please elaborate.
在不实例化对象的情况下如何在我的程序中调用它们?谁实例化隐式对象?请详细说明。
Thanks
谢谢
回答by R van Rijn
Implicit objects in JSP are the objects that are created by the container automatically and the container makes them available to the developers, the developer does not need to create them explicitly. Since these objects are created automatically by the container and are accessed using standard variables; hence, they are called implicit objects.
The implicit objects are parsed by the container and inserted into the generated servlet code. They are available only within the jspService method and not in any declaration. Implicit objects are used for different purposes. Our own methods (user defined methods) can't access them as they are local to the service method and are created at the conversion time of a jsp into a servlet. But we can pass them to our own method if we wish to use them locally in those functions.
JSP中的隐式对象是容器自动创建的对象,容器提供给开发者使用,开发者不需要显式创建。由于这些对象是由容器自动创建并使用标准变量访问的;因此,它们被称为隐式对象。
隐式对象由容器解析并插入到生成的 servlet 代码中。它们仅在 jspService 方法中可用,而不在任何声明中。隐式对象用于不同的目的。我们自己的方法(用户定义的方法)无法访问它们,因为它们是服务方法的本地方法,并且是在将 jsp 转换为 servlet 时创建的。但是如果我们希望在这些函数中本地使用它们,我们可以将它们传递给我们自己的方法。
Source: roseindia.net
回答by Rodney S. Foley
Implicit objects are a set of Java objects that the JSP Container makes available to developers in each page. These objects may be accessed as built-in variables via scripting elements and can also be accessed programmatically by JavaBeans and Servlets.
隐式对象是 JSP 容器在每个页面中提供给开发人员的一组 Java 对象。这些对象可以通过脚本元素作为内置变量访问,也可以通过 JavaBean 和 Servlet 以编程方式访问。
For a detailed overview and use please see the page below.
有关详细概述和使用,请参阅以下页面。
http://www.gulland.com/courses/JavaServerPages/jsp_objects.jsp
http://www.gulland.com/courses/JavaServerPages/jsp_objects.jsp
回答by BalusC
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 scriptletsto grab them.
这些是其中已被放置在由servlet容器的范围中的对象,因此,它的访问由EL(表达式语言),如PageContext
,HttpServletRequest#getParameter()
,HttpServletRequest#getHeader()
等。这些只是为了方便,这样您就不需要使用老式的scriptlet来获取它们。
So instead of for example
所以而不是例如
<%= 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>
you can just do
你可以做
${pageContext.session.maxInactiveInterval}<br>
${param.foo}<br>
${header['user-agent']}<br>
${cookie.foo}<br>
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.
您会看到它们遵循要访问的 JavaBean 约定(即您可以只调用 JavaBean 方式的 getter)。你看我用大括号表示法[]
来得到user-agent
,那是因为-
是 EL 中的保留字符,所以${header.user-agent}
不会工作,它会尝试返回request.getHeader("user") - pageContext.findAttribute("agent")
显然不会工作的结果。
For an overview of them all, check the chapter Implicit Objectsin the Java EE tutorial.
有关所有这些的概述,请查看Java EE 教程中的隐式对象一章。
回答by fastcodejava
Implicit objects are created automatically and are ready for you to use. You cannot create other variables with same name with these objects.
隐式对象是自动创建的,可供您使用。您不能使用这些对象创建同名的其他变量。
回答by Chinmoy Mishra
JSP Implicit Objects are the Java objects that the JSP Container makes available to developers in each page and developer can call them directly without being explicitly declared. JSP Implicit Objects are also called pre-defined variables.
JSP 隐式对象是 JSP 容器在每个页面中提供给开发人员的 Java 对象,开发人员可以直接调用它们而无需显式声明。JSP 隐式对象也称为预定义变量。
JSP supports nineImplicit Objects which are listed below:
JSP 支持以下九个隐式对象:
- request: This is the HttpServletRequestobject associated with the request.
- response: This is the HttpServletResponseobject associated with the response to the client.
- out: This is the PrintWriterobject used to write any data to buffer.
- session: This is the HttpSessionobject associated with the request.
- application: This is the ServletContextobject associated with application context.
- config: This is the ServletConfigobject associated with the page.
- pageContext: This encapsulates use of server-specific features like higher performance JspWriters.
- page: This is simply a synonym for this, and is used to call the methods defined by the translated servlet class.
- Exception: The Exception object allows the exception data to be accessed by designated JSP.
- request:这是与请求关联的HttpServletRequest对象。
- response:这是与对客户端的响应相关联的HttpServletResponse对象。
- out:这是用于将任何数据写入缓冲区的PrintWriter对象。
- session:这是与请求关联的HttpSession对象。
- application:这是与应用程序上下文关联的ServletContext对象。
- config:这是与页面关联的ServletConfig对象。
- pageContext:这封装了服务器特定功能的使用,如更高性能的 JspWriters。
- page:这只是 this 的同义词,用于调用翻译后的 servlet 类定义的方法。
- Exception:Exception 对象允许指定的 JSP 访问异常数据。
回答by pardeep131085
A JSP page can access some specific objects through scripting variables. These objects are provided by the JSP container and are called implicit objects. These implicit objects can be accessed in scriptlets, in expressions, or as part of the EL expressions.
JSP 页面可以通过脚本变量访问某些特定对象。这些对象由 JSP 容器提供,称为隐式对象。这些隐式对象可以在 scriptlet、表达式中或作为 EL 表达式的一部分进行访问。
Scope objects are
范围对象是
回答by Sreedevi Chowdary
JSP implicit objects are created by container while translating JSP page to Servlet source to help developers
JSP隐式对象由容器创建,同时将JSP页面翻译成Servlet源码,帮助开发者
回答by JegsVala
There are the nine type of implicit object, Implicit Objects are also called pre-defined variables.
1) request
2) response
3) application
4) session
5) page
6) pageContext
7) out
8) exception
9) config
隐式对象有九种类型,隐式对象也称为预定义变量。
1) 请求
2) 响应
3) 应用程序
4) 会话
5) 页面
6) pageContext
7) 输出
8) 异常
9) 配置