当前位置:网站首页>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);//true
2. Use scope for data sharing and flow
//Data storagesc1.setAttribute("num", 1);//fetch dataint num=(int) sc1.getAttribute("num");System.out.println(num);//1
3. 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.jsp
4. 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-8
or
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/html
5. 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.
边栏推荐
猜你喜欢
随机推荐
VS2022 encapsulates static libraries and calls static libraries under window
memblock
自监督论文阅读笔记Reading and Writing: Discriminative and Generative Modelingfor Self-Supervised Text Recogn
Automatic ticket issuance based on direct reduction of China Southern Airlines app
采用Trench肖特基二极管,实现功率密度的显著提升
【C语言】斐波那契数列
中空编码器的作用——唯样商城
VCC(电源)和 GND(地)之间电容的作用
g++ parameter description
自监督论文阅读笔记: MoCoV2使用动量对比学习改进基线
在大程序中怎么样显示LED点阵
电子元器件之电子变压器可分为哪几类?
ucosII OSMemCreate()函数的解析
二、Exception和Error有什么区别?
观看华为AI技术领域课程--深度学习前三章总结
树——前序
POE交换机全方位解读(中)
Phase Vocoder的补充完善,Matlab音频变速不变调、变调不变速
关于芯片你了解吗?
自监督论文阅读笔记 Self-Supervised Deep Learning for Vehicle Detection in High-Resolution Satellite Imagery