作用域(Scope)

“作用域”就是”信息共享的范围”,也就是说一个信息能够在多大的范围内有效。

Web交互的最基本单位为HTTP请求。每个用户从进入网站到离开网站这段过程称为一个HTTP会话,一个服务器的运行过程中会有多个用户访问,就是多个HTTP会话。
一、application

Application 的作用范围在服务器一开始执行服务,到服务器关闭为止Application 的范围最、停留的时间也最久,所以使用时要特别注意不然可能会造成服务器负载越来越重的情况。只要将数据存入application对象,数据的范围范围 (Scope) 就为Application。

具有application范围的对象被绑定到javax.servlet.ServletContext中。在Web应用程序运行期间,所有的页面都可以访问在这个范围内的对象。

application对象的主要方法:

1、 getAttribute(String name) return Object

2、 getAttributeNames() return Enumeration

3、 getInitParameter(String name)

4、 getServletInfo()

5、 setAttribute(String name , Object object)

二、session !!!
session:https://blog.csdn.net/qq_42577241/article/details/89556497
HTTP会话开始到结束这段时间。Session 的作用范围为一段用户持续和服务器所连接的时间,但与服务器断线,这个属性就无效了。

Session 的开始时刻比较容易判断,它从浏览器发出第一个HTTP请求即可认为会话开始。但结束时刻就不好判断了,因为浏览器关闭时并不会通知服务器,所以只能通过如下这种方法判断:如果一定的时间内客户端没有反应,则认为会话结束。Tomcat的默认值为120分钟,但这个值也可以通过HttpSession的setMaxInactiveInterval()方法来设置。

具有session范围的对象被绑定到javax.servlet.http.HttpSession对象中。

Session对象的主要方法:

1、 getAttribute(String name) return Object

2、 getAttributeNames()return Enumeration

3、 getCreationTime()return long

4、 getId()return String

5、 getLastAccessedTime() return long

6、 getMaxInactiveInterval() return int

7、 removeAttribute(String name) void

8、 setAttribute(String name , java.lang.Object value) void

三、requset !!!

HTTP请求开始到结束这段时间。Request 的范围是指在一JSP 网页发出请求到另一个JSP 网页之间,否则这个属性就失效。一个HTTP请求的处理可能需要多个Servlet合作,而这几个Servlet之间可以通过某种方式传递信息,但这个信息在请求结束后就无效了。
具有request范围的对象被绑定到javax.servlet.ServletRequest对象中。

要注意的是,因为请求对象对于每一个客户请求都是不同的,所以对于每一个新的请求,都要重新创建和删除这个范围内的对象。

request对象的主要方法:

1、 getParameter(String name) return String

2、 getParameterNames() return Enumeration

3、 getParameterValues(String name) return String[]

四、page

作用范围:当前页面从打开到关闭这段时间,它只能在同一个页面中有效。

具有page范围的对象被绑定到javax.servlet.jsp.PageContext对象中。

五、升级

request和page的生命周期都是短暂的,它们之间的区别:一个request可以包含多个page页(include,forward及filter)

为了避免与Servlet API耦合在一起,方便Action类做单元测试,Struts 2对HttpServletRequest、HttpSession和ServletContext进行了封装,构造了三个Map对象来替代这三种对象,在Action中,直接使用HttpServletRequest、HttpSession和ServletContext对应的Map对象来保存和读取数据。

要获取这三个 Map 对象,可以使用 com.opensymphony.xwork2.ActionContext 类, ActionContext 是 action 执行的上下文,在 ActionContext 中保存了 action 执行所需的一组对象,包括 parameters 、 request 、 session 、 application 和 locale 等。
ActionContext类定义了如下方法,用于获取HttpServletRequest、HttpSession和ServletContext对应的Map对象。

public Object get(Object key)

ActionContext类没有提供类似getRequest()这样的方法来获取封装了HttpServletRequest的Map对象。要得到请求Map对象,你需要为get()方法传递参数“request”。

public Map getSession()

获取封装了HttpSession的Map对象。

public Map getApplication()

获取封装了ServletContext的Map对象。

例:通过ActionContext来获取request、session和application对象的LoginAction1

package action;

ActionContext context =ActionContext.getContext();

Map request =(Map)context.get(“request”);

Map session = context.getSession();

Map application = context.getApplication();

request.put(“greeting”, “欢迎您来到程序员之家”);

session.put(“user”, user);

在成功页面中,可以使用JSP内置的表达式语言来访问request、session。、

下面我们通过几个简单的例子,来看一下这4种范围对象的应用。

1.测试page范围

test1.jsp

<%

pageContext.setAttribute(“name”,”zhangsan”);

out.println(“test1.jsp: “);

out.println(pageContext.getAttribute(“name”));

out.println(“

“);

pageContext.include(“test2.jsp”);

%>

test2.jsp

<%

out.println(“test2.jsp: “);

out.println(pageContext.getAttribute(“name”));

%>

访问test1.jsp,将看到如下的输出:

test1.jsp: zhangsan

test2.jsp: null

说明保存在pageContext对象中的属性具有page范围,只能在同一个页面中被访问。

2.测试request范围

修改test1.jsp和test2.jsp,如下所示。

test1.jsp

<%

request.setAttribute(“name”,”zhangsan”);

out.println(“test1.jsp: “);

out.println(request.getAttribute(“name”));

out.println(“

“);

pageContext.include(“test2.jsp”);

%>

test2.jsp

<%

out.println(“test2.jsp: “);

out.println(request.getAttribute(“name”));

%>

访问test1.jsp,将看到如下的输出:

test1.jsp: zhangsan

test2.jsp: zhangsan

说明保存在request对象中的属性具有request范围,在请求对象存活期间,可以访问这个范围内的对象。

将pageContext.include(“test2.jsp”); 这一句注释起来,先访问test1.jsp,再访test2.jsp,可以看到如下输出:

test2.jsp: null

这是因为客户端开始了一个新的请求。

3.测试session范围

修改test1.jsp和test2.jsp,如下所示。

test1.jsp

<%

session.setAttribute(“name”,”zhangsan”);

%>

test2.jsp

<%

out.println(“test2.jsp: “);

out.println(session.getAttribute(“name”));

%>

先访问test1.jsp,然后在同一个浏览器窗口中访问test2.jsp,可以看到如下输出:

test2.jsp: zhangsan

说明保存在session对象中的属性具有session范围,在会话期间,可以访问这个范围内的对象。

如果我们在访问完test1.jsp后,关闭浏览器,重新打开浏览器窗口,访问test2.jsp,将看到如下输出:

test2.jsp: null

这是因为客户端与服务器开始了一次新的会话。

4.测试application范围

修改test1.jsp和test2.jsp,如下所示。

test1.jsp

<%

application.setAttribute(“name”,”zhangsan”);

%>

test2.jsp

<%

out.println(“test2.jsp: “);

out.println(application.getAttribute(“name”));

%>

先访问test1.jsp,然后关闭浏览器,再打开浏览器窗口,访问test2.jsp,可以看到如下输出:

test2.jsp: zhangsan

说明保存在 application 对象中的属性具有 application 范围,在 Web 应用程序运行期间,都可以访问这个范围内的对象。

转载自:http://blog.csdn.net/u013938465/article/details/48712137

————————

“Scope” refers to the “scope of information sharing”, that is, how much information can be effective.

The most basic unit of Web interaction is HTTP request. The process of each user from entering the website to leaving the website is called an HTTP session. During the operation of a server, multiple users will access, that is, multiple HTTP sessions.
1、 Application

The scope of application is from the beginning of the server’s execution of services to the server’s shutdown. The scope of application is the largest and the stay time is the longest, so pay special attention to it when using it, otherwise it may cause more and more heavy load on the server. As long as the data is stored in the application object, the scope of the data is application.

Objects with application scope are bound to javax servlet. In ServletContext. During the running of the web application, all pages can access the objects in this range.

Main methods of application object:

1、 getAttribute(String name) return Object

2、 getAttributeNames() return Enumeration

3、 getInitParameter(String name)

4、 getServletInfo()

5、 setAttribute(String name , Object object)

2、 Session!!!
session: https://blog.csdn.net/qq_42577241/article/details/89556497
The time between the beginning and end of an HTTP session. The scope of session is a period of time that the user continues to connect with the server, but this attribute is invalid if he is disconnected from the server.

The start time of session is easy to judge. It can be regarded as the beginning of the session from the first HTTP request sent by the browser. However, it is difficult to judge the end time, because the server will not be notified when the browser is closed, so it can only be judged by the following method: if the client does not respond within a certain time, the session is considered to be over. The default value of Tomcat is 120 minutes, but this value can also be set through the setmaxinactivival() method of httpsession.

Objects with session scope are bound to javax servlet. http. Httpsession object.

Main methods of session object:

1、 getAttribute(String name) return Object

2、 getAttributeNames()return Enumeration

3、 getCreationTime()return long

4、 getId()return String

5、 getLastAccessedTime() return long

6、 getMaxInactiveInterval() return int

7、 removeAttribute(String name) void

8、 setAttribute(String name , java.lang.Object value) void

3、 Requset!!!

The period from the beginning to the end of an HTTP request. The scope of request refers to the range between a JSP Web page sending a request to another JSP Web page, otherwise this attribute will be invalid. The processing of an HTTP request may require the cooperation of multiple servlets, and these servlets can transfer information in some way, but this information is invalid after the request is completed.
Objects with request scope are bound to javax servlet. In the ServletRequest object.

It should be noted that because the request object is different for each customer request, the objects within this range should be re created and deleted for each new request.

Main methods of request object:

1、 getParameter(String name) return String

2、 getParameterNames() return Enumeration

3、 getParameterValues(String name) return String[]

4、 Page

Scope: the current page can only be valid in the same page from opening to closing.

Objects with page scope are bound to javax servlet. jsp. In the pagecontext object.

5、 Upgrade

The life cycle of request and page is short. The difference between them is that a request can contain multiple page pages (include, forward and filter)

In order to avoid coupling with servlet API and facilitate unit testing of action class, struts 2 encapsulates HttpServletRequest, httpsession and ServletContext, and constructs three map objects to replace these three objects. In action, the map objects corresponding to HttpServletRequest, httpsession and ServletContext are directly used to save and read data.

To get these three map objects, you can use COM opensymphony. xwork2. Actioncontext class. Actioncontext is the context of action execution. A set of objects required for action execution are saved in actioncontext, including parameters, request, session, application and locale.
Actioncontext class defines the following methods to obtain the map objects corresponding to HttpServletRequest, httpsession and ServletContext.

public Object get(Object key)

Actioncontext class does not provide a method like getrequest () to obtain the map object encapsulated with HttpServletRequest. To get the requested map object, you need to pass the parameter “request” for the get () method.

public Map getSession()

Get the map object that encapsulates httpsession.

public Map getApplication()

Get the map object that encapsulates the ServletContext.

例:通过ActionContext来获取request、session和application对象的LoginAction1

package action;

ActionContext context =ActionContext.getContext();

Map request =(Map)context.get(“request”);

Map session = context.getSession();

Map application = context.getApplication();

request. Put (“greeting”, “welcome to the programmer’s home”);

session.put(“user”, user);

In the success page, you can use the built-in expression language of JSP to access request and session

Let’s take a look at the application of these four range objects through several simple examples.

1. Test scope

test1.jsp

<%

pageContext.setAttribute(“name”,”zhangsan”);

out.println(“test1.jsp: “);

out.println(pageContext.getAttribute(“name”));

out.println(“

“);

pageContext.include(“test2.jsp”);

%>

test2.jsp

<%

out.println(“test2.jsp: “);

out.println(pageContext.getAttribute(“name”));

%>

Access test1 JSP, you will see the following output:

test1.jsp: zhangsan

test2.jsp: null

Description the attributes saved in the pagecontext object have page scope and can only be accessed in the same page.

2. Test request range

Modify test1 JSP and test2 JSP, as shown below.

test1.jsp

<%

request.setAttribute(“name”,”zhangsan”);

out.println(“test1.jsp: “);

out.println(request.getAttribute(“name”));

out.println(“

“);

pageContext.include(“test2.jsp”);

%>

test2.jsp

<%

out.println(“test2.jsp: “);

out.println(request.getAttribute(“name”));

%>

Access test1 JSP, you will see the following output:

test1.jsp: zhangsan

test2.jsp: zhangsan

Description: the attributes saved in the request object have a request range. During the survival of the request object, you can access the objects in this range.

Pagecontext include(“test2.jsp”); To annotate this sentence, first visit test1 JSP, visit test2 JSP, you can see the following output:

test2.jsp: null

This is because the client starts a new request.

3. Test session range

Modify test1 JSP and test2 JSP, as shown below.

test1.jsp

<%

session.setAttribute(“name”,”zhangsan”);

%>

test2.jsp

<%

out.println(“test2.jsp: “);

out.println(session.getAttribute(“name”));

%>

First visit test1 JSP, and then access test2 in the same browser window JSP, you can see the following output:

test2.jsp: zhangsan

Description the attributes saved in the session object have a session scope. During the session, you can access the objects in this scope.

If we are visiting test1 JSP, close the browser, reopen the browser window, and access test2 JSP, you will see the following output:

test2.jsp: null

This is because the client starts a new session with the server.

4. Test application scope

Modify test1 JSP and test2 JSP, as shown below.

test1.jsp

<%

application.setAttribute(“name”,”zhangsan”);

%>

test2.jsp

<%

out.println(“test2.jsp: “);

out.println(application.getAttribute(“name”));

%>

First visit test1 JSP, then close the browser, and then open the browser window to access test2 JSP, you can see the following output:

test2.jsp: zhangsan

Description the attributes saved in the application object have the application scope, and the objects in this scope can be accessed during the running of the web application.

转载自:http://blog.csdn.net/u013938465/article/details/48712137