当前位置:网站首页>ServletConfig与ServletContext
ServletConfig与ServletContext
2022-06-27 08:06:00 【汤键.TJ】
目录
ServletConfig
ServletConfig的介绍
- ServletConfig是Servlet的配置参数对象
- 在Servlet的规范中,允许为每一个Servlet都提供一些初始化的配置
- 所以,每个Servlet都有一个自己的ServletConfig
- 作用:
- 在Servlet初始化时,把一些配置信息传递给Servlet
- 生命周期:和Servlet相同
ServletConfig配置方式
- 在<servlet>标签中,通过<init-param>标签来配置;
- 有两个子标签
- 前面介绍也提到过,这些配置信息都是以键值对进行体现的
- 所以这两子标签分别是:
- <param-name>:代表初始化参数的key
- <param-value>:代表初始化参数的value

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>cpc</servlet-name> <servlet-class>com.example.servletdemo1.HelloServlet</servlet-class> <init-param> <param-name>tjrac</param-name> <param-value>2021tj</param-value> </init-param> <init-param> <param-name>hdu</param-name> <param-value>2022tj</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>cpc</servlet-name> <url-pattern>/cpc/*</url-pattern> </servlet-mapping> </web-app>ServletConfig的常用方法
- 常用方法
- getInitParameter(String name)
- 根据参数名称获取参数的值
- 返回值:String
- getInitParameterNames()
- 获取所有参数名称的枚举
- 返回值:Enumeration<String>
- getServletName()
- 获取Servlet的名称
- 返回值:String
- getServletContext()
- 获取ServletContext对象
- 返回值:ServletContext
- 使用示例
- 第一步:声明ServletConfig
- 第二步:通过重写init方法,来对ServletConfig对象进行赋值

- 第三步:演示ServletConfig常用方法


package com.example.servletdemo1; import java.io.*; import java.util.Enumeration; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.http.HttpServlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloServlet extends HttpServlet { private ServletConfig config; public void init(ServletConfig config) throws ServletException { this.config = config; } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //根据key获取value String hduvalue = config.getInitParameter("hdu"); System.out.println(hduvalue); //获取所有的key Enumeration<String> keys= config.getInitParameterNames(); while(keys.hasMoreElements()){ //获取每一个key String key= keys.nextElement(); //根据key获取value String value = config.getInitParameter(key); System.out.println(key+","+value); } //获取Servlet的名称 String servletName = config.getServletName(); System.out.println(servletName); //获取ServletContext对象 ServletContext servletContext = config.getServletContext(); System.out.println(servletContext); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }ServletContext
ServletContext的介绍
- 域对象
- 域对象指的是对象有作用域
- 也就是有作用范围
- 域对象可以实现数据的共享
- 不同作用范围的域对象,共享数据的能力也不一样
- 在Servlet规范中,一共有4个域对象
- ServletContext就是其中的一个
- 它也是web应用中最大的作用域,也叫application域
- 它可以实现整个应用之间的数据共享
- ServletContext
- ServletContext是应用上下文对象(应用域对象)
- 每一个应用中只有一个ServletContext对象
- 并不是服务于某一Servlet
- 作用:
- 可以配置和获得应用的全局初始化参数,可以实现Servlet之间的数据共享
- 生命周期:应用一加载则创建,应用被停止则销毁
ServletContext配置方式
- <web-app>就是配置文件中的根标签
- 在<web-app>标签中,通过<context-param>标签来配置
- 有两个子标签
- <param-name>:代表全局初始化参数的key
- <param-value>:代表全局初始化参数的value

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>cpc</servlet-name> <servlet-class>com.example.servletdemo1.HelloServlet</servlet-class> <init-param> <param-name>tjrac</param-name> <param-value>2021tj</param-value> </init-param> <init-param> <param-name>hdu</param-name> <param-value>2022tj</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>cpc</servlet-name> <url-pattern>/cpc/*</url-pattern> </servlet-mapping> <context-param> <param-name>globalhdu</param-name> <param-value>global2022tj</param-value> </context-param> <context-param> <param-name>globaltjrac</param-name> <param-value>global2021tj</param-value> </context-param> </web-app>ServletContext常用方法
- 常用方法
- getInitParameter(String name)
- 根据名称获取全局配置的参数
- 返回值:String
- getContextPath()
- 获取当前应用的访问虚拟目录
- 返回值:String
- getRealPath(String path)
- 根据虚拟目录来获取应用部署的磁盘绝对路径
- 返回值:String
- 使用示例
- 第一步:获取ServletContext对象
- 第二步:常用方法演示

package com.example.servletdemo1; import java.io.*; import javax.servlet.ServletContext; import javax.servlet.http.HttpServlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.获取ServletContext对象 ServletContext context = getServletContext(); //2.常用方法演示 //获取全局配置参数 String value = context.getInitParameter("globalhdu"); System.out.println(value); //获取应用的虚拟目录 String contextPath = context.getContextPath(); System.out.println(contextPath); //根据虚拟目录获取绝对路径 String realPath = context.getRealPath("/"); System.out.println(realPath); String realPath1 = context.getRealPath("WEB-INF"); System.out.println(realPath1); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }- 常用方法2
- setAttribute(String name, Object value)
- 向应用域对象中存储数据
- 返回值:void
- getAttribute(String name)
- 通过名称获取应用域对象中的数据
- 返回值:Object
- removeAttribute(String name)
- 通过名称移除应用域对象中的数据
- 返回值:void
- 使用示例2
- 在ServletContext里设置共享数据
- 在ServletConfig里获取共享数据
- 配置:

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>cpc</servlet-name> <servlet-class>com.example.servletdemo1.HelloServlet</servlet-class> <init-param> <param-name>tjrac</param-name> <param-value>2021tj</param-value> </init-param> <init-param> <param-name>hdu</param-name> <param-value>2022tj</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>cpc</servlet-name> <url-pattern>/cpc/*</url-pattern> </servlet-mapping> <servlet> <servlet-name>cpcc</servlet-name> <servlet-class>com.example.servletdemo1.config</servlet-class> </servlet> <servlet-mapping> <servlet-name>cpcc</servlet-name> <url-pattern>/cpcc</url-pattern> </servlet-mapping> <context-param> <param-name>globalhdu</param-name> <param-value>global2022tj</param-value> </context-param> <context-param> <param-name>globaltjrac</param-name> <param-value>global2021tj</param-value> </context-param> </web-app>- 使用:


public class HelloServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = getServletContext(); context.setAttribute("love","daan"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }public class config extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = getServletContext(); Object love = context.getAttribute("love"); System.out.println(love); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }- 启动服务器后,分别使用完成测试
- http://localhost:8080/demo2/cpc
- http://localhost:8080/demo2/cpcc
边栏推荐
- Time function calculation efficiency of C
- Zabbix部署说明(Server+Win客户端+交换机(H3C))
- 安装jenkins
- 關聯GIS:條條道路通UE5城
- 准备好迁移上云了?请收下这份迁移步骤清单
- 无论LCD和OLED显示技术有多好,都无法替代这个古老的显示数码管
- [c++ primer notes] Chapter 4 expression
- 爬一个网页的所有导师信息
- Testing network connectivity with the blackbox exporter
- 2022 love analysis · panoramic report of it operation and maintenance manufacturers
猜你喜欢

SPARQL basic introductory exercise

Import and export database related tables from the win command line

准备好迁移上云了?请收下这份迁移步骤清单
![[batch dos-cmd command - summary and summary] - parameters%0,%1,%2,%[0-9],%0-9 in the batch command and batch command parameter position switching command shift, operator% usage in the DOS command](/img/05/19299c47d54d4ede95322b5a923093.png)
[batch dos-cmd command - summary and summary] - parameters%0,%1,%2,%[0-9],%0-9 in the batch command and batch command parameter position switching command shift, operator% usage in the DOS command

MySQL environment variable configuration tutorial

盲测调查显示女码农比男码农更优秀

参考 | 升级 Win11 移动热点开不了或者开了连不上

JS, and output from small to large

Ready to migrate to the cloud? Please accept this list of migration steps

Common operation and Principle Exploration of stream
随机推荐
MySQL环境变量配置的教程
Cookie encryption 7 fidder analysis phase
Helix QAC is updated to 2022.1 and will continue to provide high standard compliance coverage
期货反向跟单靠谱吗?
How can I import data from Oracle into fastdfs?
JS output all prime numbers between 1-100 and calculate the total number
lvgl 说明3关于lvgl guider的使用
js求所有水仙花数
(note) Anaconda navigator flashback solution
[batch dos-cmd command - summary and summary] - map folder to virtual disk - subst
闭包问题
js成绩奖惩例题
[c++ primer notes] Chapter 4 expression
Lvgl usage demo and instructions 2
准备好迁移上云了?请收下这份迁移步骤清单
分析日志.log
第6届蓝桥杯
If xn > 0 and X (n+1) /xn > 1-1/n (n=1,2,...), Prove that the series Σ xn diverges
【批处理DOS-CMD命令-汇总和小结】-批处理命令中的参数%0、%1、%2、%[0-9]、%0-9和批处理命令参数位置切换命令shift,dos命令中操作符%用法
No matter how good LCD and OLED display technologies are, they cannot replace this ancient display nixie tube








