当前位置:网站首页>ServletContext object
ServletContext object
2022-06-12 10:12:00 【A man who is rubbed on the ground by math every day】
ServletContext object
ServletContext object
every last web There is only one application ServleContext object , also called Application object , From the name we can see , The object is application specific , stay web When the container starts , Will be for each one web The application talks about a corresponding ServletContext object .
This object has two main functions ,
First of all 、 As domain objects to share data , At this point, the data is shared throughout the application .
second 、 The object saves information about the current application . For example, through getServerInfo() Method to get the current server information ,getRealPath(String path) Get the real path of resources and so on .
ServletContext Object acquisition
obtain ServletContext There are many ways for objects . such as :
adopt request Object acquisition
ServletContext servletContext = request.getServletContext();adopt session Object acquisition
ServletContext servletContext = request.getSession().getServletContext();adopt servletConfig Object acquisition , stay Servlet The standard provides ServletConfig Method
ServletConfig servletConfig = getServletConfig(); ServletContext servletContext = servletConfig.getServletContext();Direct access to ,Servlet Class provides direct access to ServletContext Object method
ServletContext servletContext = getServletContext();
Common methods
// Get the real path of the project
String realPath = request.getServletContext().getRealPath("/");
// Get the version information of the current server
String serverInfo = request.getServletContext().getServerInfo();
ServletContext Domain object ( Not recommended )
ServletContext It can also be used as a domain object , Through to the ServletContext Data access in , You can make the entire application share certain numbers According to the . Of course, it is not recommended to store too much data , because ServletContext Once the data is stored in and not removed manually, it will always be saved .
// obtain ServletContext object
ServletContext servletContext = request.getServletContext();
// Set the domain object
servletContext.setAttribute("name","zhangsan");
// Get the domain object
String name = (String) servletContext.getAttribute("name");
// Remove domain objects
servletContext.removeAttribute("name");
Servlet Three domain objects
The larger the scope of the domain object , The more memory you use
- request The domain object is valid in one request . Request forwarding is valid , Redirection failed .
- session Domain objects are valid in one session . Request forwarding and redirection are valid ,session It is invalid after being destroyed .
- servletContext Domain objects are valid throughout the application . After the server is shut down .
边栏推荐
- C break continue return
- SAP Hana error message sys_ XSA authentication failed SQLSTATE - 28000
- 古董级MFC/GDI+框架LCD显示控件
- There is always a negative line (upper shadow line) that will stop the advance of many armies, and there is always a positive line (lower shadow line) that will stop the rampant bombing of the air for
- Auto. JS learning note 4: after autojs is packaged, most Huawei and other big brand mobile phones cannot be installed? This problem can be solved by using the simulator to remotely sign and package in
- Detailed explanation and use of redis data types: key and string types
- Spark complex structure data retrieval method
- MySQL optimized slow log query
- [cloud native | kubernetes] kubernetes networkpolicy
- Canal ha mode configuration
猜你喜欢

How to do industry analysis

MYSQL的最左匹配原则的原理讲解

Introduction to IOT

JVM (IV) Class file structure (complete parsing of bytecode attached)

tp6调试(trace)

Raw socket usage

First NFT platform in dfinity Ecology: impossible thoughts

JVM (V) Virtual machine class loading (parental delegation mechanism)

Create simple windowing programs using Visual Studio 2017

1268_ Implementation of FreeRTOS task context switching
随机推荐
redis学习记录:字典(dict)源码分析
How to implement Web3.0 and digital fashion?
7-4 network red dot punch in strategy (DFS)
Overview of software definition storage (one article is enough)
Clickhouse column basic data type description
C# break continue return 三者区别
Introduction to on-line circuit simulation and open source electronic hardware design
Papaya Mobile has a comprehensive layout of cross-border e-commerce SaaS papaya orange. What are the opportunities for this new track?
005: difference between data lake and data warehouse
spark_ sql
UE4_以现成资源探索创建背景场景的方法
Redis (I) Internal data structure
June training (day 12) - linked list
传输层协议 ——— TCP协议
SAP HANA 错误消息 SYS_XSA authentication failed SQLSTATE - 28000
【clickhouse专栏】基础数据类型说明
Basic use of scratch
【ParquetEncodingException: empty fields are illegal, the field should be ommited completely instead
[CEGUI] font loading optimization
003:what does AWS think is a data lake?