当前位置:网站首页>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.
边栏推荐
- cmdline -[command line,__fdt_pointer,initial_boot_params] boot_command_line 获取
- 自监督论文阅读笔记 S3Net:Self-supervised Self-ensembling Network for Semi-supervised RGB-D Salient Object Det
- JSP的基本使用
- classpath:与classpath*的比较
- POE交换机全方位解读(中)
- NIO知识汇总 收藏这一篇就够了!!!
- 内网渗透之PPT票据传递攻击(Pass the Ticket)
- Automatic ticket issuance based on direct reduction of China Southern Airlines app
- MATLAB自带的dwt2和wavedec2函数实现基于小波变换的自适应阈值图像边缘检测
- 神经网络基础
猜你喜欢
随机推荐
全球一流医疗技术公司如何最大程度提高设计工作效率 | SOLIDWORKS 产品探索
快速的将结构体各成员清零
cobalt strike 的基础使用
梯度下降、反向传播
ZEMAX | 如何使用ZOS-API创建自定义操作数
设备树解析源码分析<devicetree>-1.基础结构
增强光学系统设计 | Zemax 全新 22.2 版本产品现已发布!
观看华为AI技术领域课程--深度学习前三章总结
九、请介绍类加载过程,什么是双亲委派模型?
自监督论文阅读笔记 Multi-motion and Appearance Self-Supervised Moving Object Detection
深度学习理论课程第四、五章总结
ZEMAX | 在OpticStudio中建立扩增实境(VR)头戴式显示器
自监督论文阅读笔记 Self-Supervised Deep Learning for Vehicle Detection in High-Resolution Satellite Imagery
window下VS2022封装静态库以及调用静态库
ZEMAX | 在设计抬头显示器(HUD)时需要使用哪些工具?
使用JSP实现简单的登录注册功能,并且使用Session跟踪用户登录信息
自监督论文阅读笔记 TASK-RELATED SELF-SUPERVISED LEARNING FOR REMOTE SENSING IMAGE CHANGE DETECTION
MySql的Sql语句的练习(试试你能写出来几道呢)
ZEMAX | 探索 OpticStudio中的序列模式
贴片电阻的结构是怎样的?唯样商城








