当前位置:网站首页>servlet learning (7) ServletContext
servlet learning (7) ServletContext
2022-08-03 06:21:00 【Shiyu】
I. Questions
Session can solve the problem of data sharing between different requests of the same user, so what about data sharing between different users?
Second, solution
Using the ServletContext object
Three, principle
The ServletContext object is created by the server, that is to say, there is only one ServletContext object in a project, the ServletContext object obtained by different users is the same, and the ServletContext object is shared by all users.
Four. Features
The server is created, shared by users, only one per project.
Five, life cycle
Server Start->Server Shutdown
Six, scope
Within the entire project
Seven, use
1. Get the ServletContext object
//Four ways to get the ServletContext objectServletContext sc1=this.getServletContext();ServletContext sc2=req.getSession().getServletContext();ServletContext sc3=req.getServletContext();ServletContext sc4=this.getServletConfig().getServletContext();System.out.println(sc1==sc2);//trueSystem.out.println(sc1==sc3);//trueSystem.out.println(sc1==sc4);//true2. Use scope for data sharing and flow
//Data storagesc1.setAttribute("num", 1);//fetch dataint num=(int) sc1.getAttribute("num");System.out.println(num);//13. Get the absolute path of resources under WebRoot/WebContent
//Get the absolute path of resources under WebContentString path=sc1.getRealPath("1.jsp");System.out.println(path);//F:\Etomcat\webapps\018-ServletXuexi\1.jsp4. Get the global configuration in web.xml
Role: Decoupling static data and code
First configure the data in web.xml as follows:
Note: Only one key-value pair can be stored between a
charset utf-8 type text/html //Get the global configuration in web.xmlString type=sc1.getInitParameter("type");System.out.println(type);//text/htmlString charset=sc1.getInitParameter("charset");System.out.println(charset);//utf-8or
Enumeration enumeration=sc1.getInitParameterNames();while(enumeration.hasMoreElements()) {String name=(String) enumeration.nextElement();String value=sc1.getInitParameter(name);System.out.println(name+" "+value);}// output//charset utf-8//type text/html5. Get the project resource stream object under webroot/webcontent
InputStream inputStream=sc1.getResourceAsStream("1.jsp");System.out.println(inputStream);//[email protected]Note: This method can only obtain the resource stream object in the project root directory, and the class stream object needs to be obtained by using the class loader.
边栏推荐
- @JsonProperty和JSONField的区别?
- ARMv8 架构----armv8 类别
- 移动端人脸风格化技术的应用
- 虚拟地址空间布局
- 6. What is the difference between Vector, ArrayList and LinkedList?(design, performance, safety)
- 三、final、finally、 finalize有什么不同?
- MATLAB自带的dwt2和wavedec2函数实现基于小波变换的自适应阈值图像边缘检测
- MMU 介绍-[TBL/page table work]
- Windos 内网渗透之Token的使用
- 自监督论文阅读笔记 Incremental-DETR:Incremental Few-Shot Object Detection via Self-Supervised Learning
猜你喜欢

贴片电阻的结构是怎样的?唯样商城

电子元器件和电子元件的区别有那些?

芯片解密工作应该具备哪些条件?唯样商城

自监督论文阅读笔记SELF-SUPERVISED SPECTRAL MATCHING NETWORK FOR HYPERSPECTRAL TARGET DETECTION

常见的电容器有哪些?唯样商城

中空编码器的作用——唯样商城

cobalt strike 的基础使用

自监督论文阅读笔记 DetCo: Unsupervised Contrastive Learning for Object Detection

自监督论文阅读笔记 Self-Supervised Visual Representation Learning with Semantic Grouping

梯度下降、反向传播
随机推荐
ZEMAX | 在OpticStudio中建立扩增实境(VR)头戴式显示器
自监督论文阅读笔记 Multi-motion and Appearance Self-Supervised Moving Object Detection
classpath: comparison with classpath*
VS2022 encapsulates static libraries and calls static libraries under window
五、int和Integer有什么区别?
九、请介绍类加载过程,什么是双亲委派模型?
自监督论文阅读笔记Index Your Position: A Novel Self-Supervised Learning Method for Remote Sensing Images Sema
A.1#【内存管理】——1.1.3 page: struct page
MATLAB自带的dwt2和wavedec2函数实现基于小波变换的自适应阈值图像边缘检测
ZEMAX | 如何创建简单的非序列系统
Dynamic adjustment subject web system?Look at this one is enough
POE交换机全方位解读(中)
Dynamic adjustment of web theme (2) Extraction
二、Exception和Error有什么区别?
ZEMAX | 探索 OpticStudio中的序列模式
常见的电子元器件分类介绍
内网渗透之PPT票据传递攻击(Pass the Ticket)
ZEMAX | 在 OpticStudio 中使用自由曲面进行设计
二分查找3 - 猜数字大小
VCC(电源)和 GND(地)之间电容的作用