ServletConfig
ServletConfig类是servlet程序的配置信息类
ServletConfig类的三大作用
-
获取Servlet程序的别名servlet-name值
-
获取初始化参数 init-parm
-
获取ServletContext对象
@Override
public void init(ServletConfig servletConfig) throws ServletException { // 2
System.out.println("2.初始化方法");
// 获取servlet程序的别名servlet-name的值
System.out.println("HelloServlet程序的别名是(默认是类名):"+servletConfig.getServletName());
// 获取初始化参数init-param
System.out.println("初始化参数username的值是:"+servletConfig.getInitParameter("username"));
System.out.println("初始化参数url的值是:"+ servletConfig.getInitParameter("url"));
// 获取ServletContext对象
System.out.println(servletConfig.getServletContext());
}
<init-param>
<param-name>username</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://locakhost:3306/test</param-value>
</init-param>
ServletContext
-
ServletContext上下文对象
-
一个web工程,只有一个servletContext对象。
-
ServletContext是一个域对象。(像Map一样存取数据的对象,存取数据的操作范围)
Map存数据put()取数据get()删除数据remove()
域对象存数据setAttribute()取数据getAttribute()删除数据removeAttribute() -
未完待续。
ServletContext的四大作用
- 获取web.xml中配置的上下文参数context-param
- 获取当前的工程路径,格式:/工程路径
- 获取工程部署后在服务器硬盘上的绝对路径
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取配置的上下文参数(通过Config获取)
ServletContext servletContext = getServletConfig().getServletContext();
String username = servletContext.getInitParameter("username");
System.out.println("context-param参数username的值是"+username);
String password = servletContext.getInitParameter("password");
System.out.println("context-param参数password的值是"+password);
System.out.println("当前工程路径:"+servletContext.getContextPath());
System.out.println("获取工程部署在服务器硬盘上的绝对路径"+servletContext.getRealPath("/"));
}
- 像Map一样存取数据
重点:这里创建2个类继承HTTPServlet类实现Servlet程序
**以下示例Context体现出全局作用域:
一.首先创建第一个Servlet程序ContextServletAttribute
代码如下:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ServletContext servletContext = getServletConfig().getServletContext(); // 法一:通过获取ServletConfig获取ServletContext对象
ServletContext servletContext = getServletContext();
System.out.println("保存前contextServletAttribute中获取域数据key1的值是:"+servletContext.getAttribute("key1"));
servletContext.setAttribute("key1","value1");
System.out.println("contextServletAttribute中获取域数据key1的值是:"+servletContext.getAttribute("key1"));
}
二.其次创建第二个Servlet程序ContextServlet2
代码如下:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = getServletContext();
System.out.println("ContextServlet2类中获取域数据key1的值为:"+servletContext.getAttribute("key1"));
}
三.启动Tomcat服务器,网址输入http://localhost:8080/contextServletAttribute
服务器内容显示如下:
[2020-11-06 12:18:20,195] Artifact fixed_servlet: Artifact is being deployed, please wait…
[2020-11-06 12:18:20,778] Artifact fixed_servlet: Artifact is deployed successfully
[2020-11-06 12:18:20,778] Artifact fixed_servlet: Deploy took 583 milliseconds
保存前contextServletAttribute中获取域数据key1的值是:null
contextServletAttribute中获取域数据key1的值是:value1
解释:
保存前contextServletArttribute因为还没有用户域对象存数据setAttribute(),故获取不到key1的值value1
然而再输入http://localhost:8080/contextServlet2
服务器内容显示如下:
ContextServlet2类中获取域数据key1的值为:value1
解释:这里证明Context作用域为全局的,全局都能访问域对象存的数据
最后,再输入http://localhost:8080/contextServletAttribute
服务器内容显示如下:
保存前contextServletAttribute中获取域数据key1的值是:value1
contextServletAttribute中获取域数据key1的值是:value1
解释:看到了嘛,保存是有值的,不为null哦。
这里通过在一个servlet类调用setAttribute()方法,在另一个servlet程序中仍能通过Context的全局作用域特性获取到对应的值。