当前位置:网站首页>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.
边栏推荐
- NIO知识汇总 收藏这一篇就够了!!!
- 002_旭日X3派初探:TogetherROS安装
- Phase Vocoder的补充完善,Matlab音频变速不变调、变调不变速
- 自监督论文阅读笔记Reading and Writing: Discriminative and Generative Modelingfor Self-Supervised Text Recogn
- VSCODE 常见问题
- ZEMAX | 如何使用渐晕系数
- 深度学习理论课程第八、九、十章总结
- Makefile自动推导的简单例程
- 二叉树常见的问题和解决思路
- 自监督论文阅读笔记 S3Net:Self-supervised Self-ensembling Network for Semi-supervised RGB-D Salient Object Det
猜你喜欢

设备树解析源码分析<devicetree>-1.基础结构

ZEMAX | 如何使用ZOS-API创建自定义操作数

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

自监督论文阅读笔记Efficient Self-supervised Vision Pretraining with Local Masked Reconstruction

ZEMAX | How to rotate any element around any point in space

剑指 Offer II 001. 整数除法

window下VS2022封装静态库以及调用静态库

数组与字符串10-实现 strStr()

数组与字符串9-翻转字符串里的单词

自监督论文阅读笔记 Self-supervised Label Augmentation via Input Transformations
随机推荐
cb板上常用的电子元器件都有哪些?
2021-03-22
ZEMAX | 探索 OpticStudio中的序列模式
ZEMAX | 在 OpticStudio 中使用自由曲面进行设计
memblock
剑指 Offer II 001. 整数除法
A.1#【内存管理】——1.1.1 node:struct pglist_data
树——二叉排序树(BST)
关于芯片你了解吗?
【第二周】卷积神经网络
基于南航app直减自动出票
队列方法接收串口的数据
自监督论文阅读笔记Reading and Writing: Discriminative and Generative Modelingfor Self-Supervised Text Recogn
Eight, the difference between the interface of the abstract class
设备树(devicetree)-dts语法
常见的电子元器件分类介绍-唯样商城
电容器和电池有什么不同?
自监督论文阅读笔记 DetCo: Unsupervised Contrastive Learning for Object Detection
Convolutional Nerual Nertwork(CNN)
观看华为AI技术领域课程--深度学习前三章总结