当前位置:网站首页>We have to understand the four scopes: application, session, request and page
We have to understand the four scopes: application, session, request and page
2022-07-24 18:23:00 【tongle_ deng】
First of all, I want to make a statement , So-called " Scope " Namely " Scope of information sharing ", In other words, how much information can be effective .
Web The basic unit of interaction is HTTP request . The process of each user from entering the website to leaving the website is called HTTP conversation , During the operation of a server, multiple users will access , That's more than one. HTTP conversation .
One 、application
Application The scope of the server is to execute the service at the beginning of the server , Until the server is shut down Application The most extensive 、 Stay the longest , Therefore, pay special attention when using, otherwise the server load may become heavier and heavier . Just store the data in application object , Range of data (Scope) for Application.
have application The object of the scope is bound to javax.servlet.ServletContext in . stay Web While the application is running , All pages can access objects in this range .
application The main method of object :
1、 getAttribute(String name) return Object
2、 getAttributeNames() return Enumeration
3、 getInitParameter(String name)
4、 getServletInfo()
5、 setAttribute(String name , Object object)
HTTP conversation From the beginning to the end .Session The scope of the function is a period of time that the user continues to connect with the server , But with service Breaker disconnection , This property is invalid .
Session It's easier to judge the beginning of , It sends out the first HTTP The request is considered the beginning of the conversation . But the end is hard to judge , Because the server is not notified when the browser closes , Therefore, 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 .Tomcat The default value is 120 minute , But this value can also be passed through HttpSession Of setMaxInactiveInterval() Method to set .
have session The object of the scope is bound to javax.servlet.http.HttpSession In the object .
Session The main method of 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、 ... and 、requset !!!
HTTP The time between the beginning and the end of the request .Request The scope of refers to in a JSP A web page sends a request to another JSP Between pages , otherwise This property is invalidated . One HTTP The processing of the request may require multiple Servlet cooperation , And these Servlet Information can be transmitted in some way , But this information is invalid after the request is completed .
have request The object of the scope is bound to javax.servlet.ServletRequest In the object .
It should be noted that , Because the request object is different for each customer request , So for every new request , All objects in this range must be re created and deleted .
request The main method of object :
1、 getParameter(String name) return String
2、 getParameterNames() return Enumeration
3、 getParameterValues(String name) return String[]
Four 、page
Scope of action : The time between opening and closing the current page , It can only be valid on the same page .
have page The object of the scope is bound to javax.servlet.jsp.PageContext In the object .
5、 ... and 、 upgrade
request and page The life cycle of life is short , The difference between them : One request It can contain more than one page page (include,forward And filter)
In order to avoid and Servlet API Couple together , convenient Action Class for unit testing ,Struts 2 Yes HttpServletRequest、HttpSession and ServletContext It was packaged , Three Map object To replace these three objects , stay Action in , Use it directly HttpServletRequest、HttpSession and ServletContext Corresponding Map Object to save and read data .
To get these three Map object , have access to com.opensymphony.xwork2.ActionContext class ,ActionContext yes action Execution context , stay ActionContext It's preserved in action A set of objects required for execution , Include parameters、request、session、application and locale etc. .ActionContext Class defines the following methods , Used to get HttpServletRequest、HttpSession and ServletContext Corresponding Map object .
public Object get(Object key)
ActionContext Class does not provide anything like getRequest() Such a method to obtain the encapsulation HttpServletRequest Of Map object . To get a request Map object , You need to get() Method pass parameter “request”.
public Map getSession()
Get encapsulated HttpSession Of Map object .
public Map getApplication()
Get encapsulated ServletContext Of Map object .
example : adopt ActionContext To get request、session and application Object's 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 house ");
session.put("user", user);
On the success page , have access to JSP Built in expression language to access request、session.、
Let's use a few simple examples , Take a look at this 4 Application of a range object .
1. test page Range
test1.jsp
<%
pageContext.setAttribute("name","zhangsan");
out.println("test1.jsp: ");
out.println(pageContext.getAttribute("name"));
out.println("<p>");
pageContext.include("test2.jsp");
%>
test2.jsp
<%
out.println("test2.jsp: ");
out.println(pageContext.getAttribute("name"));
%>
visit test1.jsp, You will see the following output :
test1.jsp: zhangsan
test2.jsp: null
The instructions are saved in pageContext The attribute in the object has page Range , 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("<p>");
pageContext.include("test2.jsp");
%>
test2.jsp
<%
out.println("test2.jsp: ");
out.println(request.getAttribute("name"));
%>
visit test1.jsp, You will see the following output :
test1.jsp: zhangsan
test2.jsp: zhangsan
The instructions are saved in request The attribute in the object has request Range , During the lifetime of the request object , You can access objects in this range .
take pageContext.include("test2.jsp"); This sentence is annotated , First visit test1.jsp, Revisiting 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 in Same browser window Medium visit test2.jsp, You can see the following output :
test2.jsp: zhangsan
The instructions are saved in session The attribute in the object has session Range , During the conversation , You can access objects in this range .
If we finish our visit test1.jsp after , Close the browser , Reopen the browser window , visit 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 Range
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 , Then open the browser window , visit test2.jsp, You can see the following output :
test2.jsp: zhangsan
The instructions are saved in application The attribute in the object has application Range , stay Web While the application is running , Can access objects in this range .边栏推荐
- web渗透经验汇总ing
- Three ways of redis cluster
- Growth of operation and maintenance Xiaobai - week 8 of Architecture
- Laravel notes - RSA encryption of user login password (improve system security)
- 排序的几种方式for while 还有sort
- pycharm配置opencv库
- File upload vulnerability -.User.ini and.Htaccess
- Get the original data API on 1688app
- 手写博客平台~第二天
- Highcharts chart and report display, export data
猜你喜欢

Maximum sum and promotion of continuous subarrays (2)

Go language file operation

Three ways of redis cluster

pycharm配置opencv库

缺失值处理

【“码”力全开,“章”显实力】2022年第1季Task挑战赛贡献者榜单

Mid year inventory | in 2022, PAAS will be upgraded again
![[opencv] - thresholding](/img/4e/88c8c8063de7cb10e44e76e77dbb8e.png)
[opencv] - thresholding

In depth analysis of the famous Alibaba cloud log4j vulnerability

9. BOM object?
随机推荐
模拟实现vector
4. Basic type and reference type?
undefined reference to H5PTopen
颜色的13 个必备方法!
Custom web framework
缺失值处理
【obs】依赖库: x264 vs 构建
继承与派生
IO multiplexing
Three ways of redis cluster
Typora 它依然是我心中的YYDS 最优美也是颜值最高的文档编辑神器 相信你永远不会抛弃它
Four ways of simple interest mode
odoo中的bom理解
5. Reference type and value type as function parameters?
MySQL configuration file
Simulation implementation vector
Common methods of string (2)
Pytorch的旅程一:线性模型
JMeter -- silent operation
Laravel notes - RSA encryption of user login password (improve system security)