当前位置:网站首页>Servlet---ServletConfig类使用介绍
Servlet---ServletConfig类使用介绍
2022-08-08 04:31:00 【我爱布朗熊】
目录
①获取web.xml中配置的上下文参数context-param
一、ServletConfig类使用介绍
ServletConfig类从类名来看,就知道是Servlet程序的配置信息类
Servlet程序和ServletConfig对象都是由Tomcat负责创建,我们负责使用
Servlet程序默认是第一次访问的时候创建的,ServletConfig是每个Servlet程序创建时,就创建一个对应的ServletConfig对象
如果是重写了init()方法,需要自己写一个super.init(config)要不然会出现空指针异常

1.ServletConfig类的三大作用
①可以获取Servlet程序的别名servlet-name的值
②获取初始化参数init-para
③获取ServletContext对象·
@Override
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("2 初始化方法");
// 可以获取Servlet程序的别名servlet-name的值
System.out.println("别名:"+servletConfig.getServletName());
// 获取初始化参数init-para
System.out.println("init-para初始化参数username的值是:"+servletConfig.getInitParameter("username"));
System.out.println("init-para初始化参数url的值是:"+servletConfig.getInitParameter("url"));
// 获取ServletContext对象·
System.out.println(servletConfig.getServletContext());
}
<!-- servlet标签给Tomcat配置Servlet程序-->
<servlet>
<!-- 给Servlet程序起一个别名(一般是类名) -->
<servlet-name>HelloServlet2</servlet-name>
<!-- 是Servlet全类名 -->
<servlet-class>com.company.web_tomcat.HelloServlet2</servlet-class>
<!-- init-param是初始化参数(可以配置多组)-->
<init-param>
<!-- 参数名-->
<param-name>username</param-name>
<!-- 参数值 -->
<param-value>root</param-value>
</init-param>
<!-- init-param是初始化参数(可以配置多组)-->
<init-param>
<!-- 参数名-->
<param-name>url</param-name>
<!-- 参数值 -->
<param-value>jdbc:mysql://localhost:3306/test</param-value>
</init-param>
</servlet>
<!-- servlet-mapping标签给Servlet程序配置一个访问地址,如果不加这个标签的话,上面标签servlet-name便会报错-->
<servlet-mapping>
<!-- 此标签的作用是告诉服务器是告诉服务器,我当前配置的地址是给哪个Servlet程序使用 -->
<servlet-name>HelloServlet2</servlet-name>
<!-- 配置访问地址
/ 斜杠在服务器解析的时候,表示地址为:http://ip:port/工程路径
/hello http://ip:port/工程路径/hello -->
<url-pattern>/hello</url-pattern>
</servlet-mapping>

二、ServletContext类
1.什么是ServletContext?
是一个接口,表示Servlet上下文对象
一个web工程,只有一个ServletContext对象实例
ServletContext对象是一个域对象
ServletContext是在web工程部署启动的时候创建,在web工程停止的时候销毁
2.什么是域对象
是可以像Map一样存取数据的对象,叫域对象
这里的域指的是存取数据的操作范围---整个web工程

3.ServletContex类的四个作用
①获取web.xml中配置的上下文参数context-param
②获取当前的工程路径,格式:/工程路径
③获取工程部署后在服务器硬盘上的绝对路径
<servlet>
<servlet-name>ContextServlet</servlet-name>
<servlet-class>com.company.web_tomcat.ContextServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ContextServlet</servlet-name>
<url-pattern>/ContextServlet</url-pattern>
</servlet-mapping>
<!--context-param是上下文参数(它属于整个web工程(也可以配置多组) -->
<context-param>
<param-name>username</param-name>
<param-value>context</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>root</param-value>
</context-param>public class ContextServlet extends HttpServlet {
//④像Map一样存取数据
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ①获取web.xml中配置的上下文参数context-param
ServletContext context = getServletConfig().getServletContext();
String username = context.getInitParameter("username");
System.out.println("username:" + username);
System.out.println("password:" + context.getInitParameter("password"));
//②获取当前的工程路径,格式:/工程路径
System.out.println("当前工程路径:" + context.getContextPath());
//③获取工程部署后在服务器硬盘上的绝对路径
// / 斜杠被服务器解析地址为:http://ip:port/工程名/ 映射到IDEA代码的web目录
System.out.println("工程部署的绝对路径:" + context.getRealPath("/"));
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
④像Map一样存取数据
只要有ServletContext对象,并且保存了数据,不论在哪个地方,都可以取到(前提是在一个web工程中,且这个web工程没有被销毁)
public class ContextServlet1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ServletContext context = getServletConfig().getServletContext();
// 用下面获取ServletContext对象这种方法更简洁
ServletContext context = getServletContext();
context.setAttribute("key1","value1");
System.out.println("Context1中获取域数据key1的值是:"+context.getAttribute("key1"));
System.out.println("Context1中获取域数据key1的值是:"+context.getAttribute("key1"));
System.out.println("Context1中获取域数据key1的值是:"+context.getAttribute("key1"));
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
边栏推荐
- KD树应用汇总
- Codeforces Round #684 (Div. 1)
- 【Template Engine】velocity
- New ToDesk Enterprise Edition | Ten new features to make enterprise remote control safer, more convenient and smoother
- Implementing Express middleware principles
- 失业在家的6个月,我通过外包全款买了房:你看不起的行业,往往很赚钱
- Open3D 基于颜色的ICP配准
- Risk control strategy must be learned | This method of mining rules with decision trees
- Lecture 84 Biweekly t4 6144 and Lecture 305 t4 6138
- 机器学习笔记:学习率预热 warmup
猜你喜欢

亚马逊云科技Build On学习心得

y90.第六章 微服务、服务网格及Envoy实战 -- 服务网格基础(一)

NetCore uses Dapper to query data

MySql入门教程

leetcode 112. Path sum recursion

Leetcode78. 子集

机器学习笔记:学习率预热 warmup

L3-006 迎风一刀斩

The fledgling Xiao Li's 115th blog project notes on the creation of the domestic GD32F103RCT6 basic project

向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!
随机推荐
二维码生成工具
ES6剩余参数的使用
Lecture 84 Biweekly t4 6144 and Lecture 305 t4 6138
6G-Oriented Communication Perception Integrated Architecture and Key Technologies
【冷启动】快手《POSO: Personalized Cold Start Modules for Large-scale Recommender Systems》
Let your text be seen by more people: Come and contribute, the payment is reliable!
奇怪的魔法(组合数)
y90.第六章 微服务、服务网格及Envoy实战 -- 服务网格基础(一)
使用 Presto 和 Alluxio 在 AWS 上搭建高性能平台来支持实时游戏服务
Redis persistence mechanism, master-slave, sentry, cluster parsing cluster solution
【opencv】opencv开发包简介
Risk control strategy must be learned | This method of mining rules with decision trees
leetcode-同构字符串判断
torch.view()函数用法
牛客多校第6场赛后学习 B(两种做法)G(两种做法)M(两种写法)J
[Graph Basics] How to Define Few-Shot Learning on Heterogeneous Graphs: Heterogeneous Graph Few-Shot Learning
leetcode: 874. Simulate a walking robot
This article will give you a thorough understanding of synchronized and Lock
2022/08/06 Study Notes (day24) Collection
【Win10】若干睡眠问题及对策