目录
-
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
转载:https://blog.csdn.net/weixin_59624686/article/details/125456078
查看评论