Java 页面范围-jsp中的范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22766332/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:46:28  来源:igfitidea点击:

Page scope- scope in jsp

javajspservlets

提问by a Learner

There are following scopes in jsp:

jsp中有以下作用域:

page scope

页面范围

request scope

请求范围

session scope

会话范围

and application scope.

和适用范围。

I am confused about page scope. Can anybody tell me what is this page scope? I have not found its clear definition anywhere.

我对页面范围感到困惑。谁能告诉我这个页面的范围是什么?我没有在任何地方找到它的明确定义。

回答by Prabhaker A

pagescope means, it can be thought of as an object that represents the entire JSP page,i.e. the JSP object can be accessed only from within the same page where it was created.
The page object is really a direct synonym for the thisobject.
Note:

page范围意味着,它可以被认为是代表整个 JSP 页面的对象,即 JSP 对象只能从创建它的同一页面内访问。
页面对象实际上是对象的直接同义词this
注意

The main difference between pagescope and requestscope(often confusing ) is that page scope attributes are no longer available if the request is forwarded to another JSP page where as request scope attributes are available.

页面范围和请求范围之间的主要区别(经常令人困惑)在于,如果请求被转发到另一个 JSP 页面,其中请求范围属性可用,则页面范围属性将不再可用。

回答by Paul Vargas

The pagescope indicates that, in addition to being bound to a local variable, the bean object should be placed in the javax.servlet.jsp.PageContextobject for the duration of the current request.

page范围表示,除了被绑定到本地变量,豆对象应该被放置在javax.servlet.jsp.PageContext对象为当前请求的持续时间。

Acording to Allamaraju (2004):

根据 Allamaraju (2004):

JSP defines four scopes for the objects that can be used by the JSP authors:

+-------------+------------------------------------------------------+
| Scope       | Description                                          |
+-------------+------------------------------------------------------+
| page        | Objects can be accessed only within the JSP page     |
|             | in which they are referenced.                        |
+-------------+------------------------------------------------------+
| request     | Objects can be accessed within all the pages that    |
|             | serve the current request. These include pages       |
|             | that are forwarded to, and included in, the original |
|             | JSP page to which the request was routed.            |
+-------------+------------------------------------------------------+
| session     | Objects can only be accessed within the JSP pages    |
|             | accessed within the session for which the objects    |
|             | are defined.                                         |
+-------------+------------------------------------------------------+
| application | Application scope objects can be accessed by all     |
|             | JSP pages in a given context.                        |
+-------------+------------------------------------------------------+

JSP 为 JSP 作者可以使用的对象定义了四个范围:

+-------------+------------------------------------------------------+
| Scope       | Description                                          |
+-------------+------------------------------------------------------+
| page        | Objects can be accessed only within the JSP page     |
|             | in which they are referenced.                        |
+-------------+------------------------------------------------------+
| request     | Objects can be accessed within all the pages that    |
|             | serve the current request. These include pages       |
|             | that are forwarded to, and included in, the original |
|             | JSP page to which the request was routed.            |
+-------------+------------------------------------------------------+
| session     | Objects can only be accessed within the JSP pages    |
|             | accessed within the session for which the objects    |
|             | are defined.                                         |
+-------------+------------------------------------------------------+
| application | Application scope objects can be accessed by all     |
|             | JSP pages in a given context.                        |
+-------------+------------------------------------------------------+

Storing the object there means that servlet code can access it by calling getAttributeon the predefined pageContextvariable. Since every page and every request has a different PageContextobject, this indicates that the bean is not shared and thus a new bean will be created for each request.

将对象存储在那里意味着 servlet 代码可以通过调用getAttribute预定义的pageContext变量来访问它。由于每个页面和每个请求都有一个不同的PageContext对象,这表明 bean 不是共享的,因此将为每个请求创建一个新 bean。

See more in JSP Tutorial. Servlet Tutorial. Beginning and Intermediate-Level.

JSP 教程中查看更多信息小程序教程。初级和中级



References

参考

Allamaraju, S. (2004). Professional Java Servlets 2.3.Berkeley, Calif: Apress.

Allamaraju, S. (2004)。专业 Java Servlet 2.3。加州伯克利:Apress。