当前位置:网站首页>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.
边栏推荐
猜你喜欢
KASLR-内核地址空间布局随机化
常见的电容器有哪些?唯样商城
二分查找6 - 寻找峰值
增强光学系统设计 | Zemax 全新 22.2 版本产品现已发布!
2-php学习笔记之控制语句,函数
自监督论文阅读笔记 Self-supervised Label Augmentation via Input Transformations
自监督论文阅读笔记 S3Net:Self-supervised Self-ensembling Network for Semi-supervised RGB-D Salient Object Det
电子元器件之电子变压器可分为哪几类?
ZEMAX | 在设计抬头显示器(HUD)时需要使用哪些工具?
001_旭日X3派初探:开箱测试
随机推荐
中空编码器的作用——唯样商城
自监督论文阅读笔记SELF-SUPERVISED SPECTRAL MATCHING NETWORK FOR HYPERSPECTRAL TARGET DETECTION
全球一流医疗技术公司如何最大程度提高设计工作效率 | SOLIDWORKS 产品探索
自监督论文阅读笔记FIAD net: a Fast SAR ship detection network based on feature integration attention and self
二、Exception和Error有什么区别?
最优化方法概述
内网渗透之PPT票据传递攻击(Pass the Ticket)
自监督论文阅读笔记 Self-supervised Label Augmentation via Input Transformations
JS--正则表达式
数组与字符串13-两数之和等于目标数
A.1#【内存管理】——1.1.1 node:struct pglist_data
【C语言】关于数组传参问题/首地址
MATLAB给多组条形图添加误差棒
ZEMAX | 如何创建简单的非序列系统
003_旭日X3派初探:利用无线串口通信控制舵机
建立平衡二叉树简单demo
增强光学系统设计 | Zemax 全新 22.2 版本产品现已发布!
使用JSP实现简单的登录注册功能,并且使用Session跟踪用户登录信息
常见的电子元器件分类介绍-唯样商城
剑指 Offer II 001. 整数除法