14. Servlet入门 - ServletContext类作为全局域对象共享数据使用

网友投稿 582 2022-11-22

14. Servlet入门 - ServletContext类作为全局域对象共享数据使用

14. Servlet入门 - ServletContext类作为全局域对象共享数据使用

14. Servlet入门 - ServletContext类作为全局域对象共享数据使用

ServletContext 类

什么是 ServletContext?

2、一个 web 工程,只有一个 ServletContext 对象实例。

3、ServletContext 对象是一个域对象。

4、ServletContext 是在 web 工程部署启动的时候创建。在 web 工程停止的时候销毁。

什么是域对象?

域对象,是可以像 Map 一样存取数据的对象,叫域对象。

这里的域指的是存取数据的操作范围,整个 web 工程。

存数据 取数据 删除数据 Map put() get() remove() 域对象 setAttribute() getAttribute() removeAttribute();

ServletContext 类的作用

1、获取 web.xml 中配置的上下文参数 context-param

2、获取当前的工程路径,格式: /工程路径

3、获取工程部署后在服务器硬盘上的绝对路径

4、像 Map 一样存取数据

5、获得文件mini类型(文件-)

6、获取web资源路径  ,可以将Web资源转换成字节输入流(掌握)

获取 web.xml 上下文、工程路径、部署路径

1.首先创建一个新的Servlet用来测试

import javax.servlet.ServletException;import javax.servlet.javax.servlet.javax.servlet.java.io.IOException;/** * @author Aron.li * @date 2020/11/12 8:18 */public class ContextServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }}

2.配置该Servlet 的 XML 以及 上下文参数 context-param

username root password 123abc ContextServlet com.test01.ContextServlet ContextServlet /context

3.获取 web.xml 中配置的上下文参数 context-param

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("ContextServlet的Get方法:"); //1. 获取 web.xml 中配置的上下文参数 context-param ServletContext context = getServletContext(); // 获取ServletContext对象 // 获取上下文username参数 String username = context.getInitParameter("username"); System.out.println("context-param参数username的值是:" + username); // 获取上下文password参数 String password = context.getInitParameter("password"); System.out.println("context-param参数password的值是:" + password);}

4.获取当前的工程路径,格式: /工程路径

// 获取ServletContext对象ServletContext context = getServletContext(); //2.获取当前的工程路径,格式: /工程路径System.out.println("当前工程路径:" + context.getContextPath());

5. 获取工程部署后在服务器硬盘上的绝对路径

5.1 获取工程的根路径部署地址

//3. 获取工程部署后在服务器硬盘上的绝对路径/** * / 斜杠被服务器解析地址为: 映射到IDEA代码的web目录
*/System.out.println("工程部署的路径是:" + context.getRealPath("/"));

我们可以看到打印硬盘部署路径,可以打开该路径看看,如下:

5.2 除了获取工程的根路径,还可以任意拼接其他路径,就算该路径在硬盘中并不存在

// 除了获取工程的根路径,还可以任意拼接其他路径,就算该路径在硬盘中并不存在System.out.println("工程下css目录的绝对路径是:" + context.getRealPath("/css"));System.out.println("工程下imgs目录1.jpg的绝对路径是:" + context.getRealPath("/imgs/1.jpg"));

6.演示代码

ServletContext 演示代码:

package com.test01;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.javax.servlet.javax.servlet.java.io.IOException;/** * @author Aron.li * @date 2020/11/12 8:18 */public class ContextServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("ContextServlet的Get方法:"); //1. 获取 web.xml 中配置的上下文参数 context-param ServletContext context = getServletContext(); // 获取ServletContext对象 // 获取上下文username参数 String username = context.getInitParameter("username"); System.out.println("context-param参数username的值是:" + username); // 获取上下文password参数 String password = context.getInitParameter("password"); System.out.println("context-param参数password的值是:" + password); //2.获取当前的工程路径,格式: /工程路径 System.out.println("当前工程路径:" + context.getContextPath()); //3. 获取工程部署后在服务器硬盘上的绝对路径 /** * / 斜杠被服务器解析地址为: 映射到IDEA代码的web目录
*/ System.out.println("工程部署的路径是:" + context.getRealPath("/")); // 除了获取工程的根路径,还可以任意拼接其他路径,就算该路径在硬盘中并不存在 System.out.println("工程下css目录的绝对路径是:" + context.getRealPath("/css")); System.out.println("工程下imgs目录1.jpg的绝对路径是:" + context.getRealPath("/imgs/1.jpg")); }}

web.xml 中的配置

username root password 123abc ContextServlet com.test01.ContextServlet ContextServlet /context

ServletContext 像 Map 一样存取数据

1.创建一个 ContextServlet1 来进行演示

public class ContextServlet1 extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }}

配置 web.xml 如下:

ContextServlet1 com.test01.ContextServlet1 ContextServlet1 /context1

2.在 ContextServlet1 存储数据 以及 读取数据

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取ServletContext对象 ServletContext context = getServletContext(); //在 ContextServlet1 存储数据 以及 读取数据 context.setAttribute("key1", "value1"); // 存储数据 System.out.println("ContextServlet1 读取数据: " + context.getAttribute("key1")); // 读取数据}

3.启动 tomcat 服务,请求测试如下:

可以看到能够读取数据。另外,context 存储的数据是共享于整个 web 工程的,也就是说其他的 Servlet 程序也是可以读取的。

4.再创建一个 ContextServlet2 ,尝试读取 ContextServlet1 存储的值

public class ContextServlet2 extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取ServletContext对象 ServletContext context = getServletContext(); //在 ContextServlet2 读取数据 System.out.println("ContextServlet2 读取数据: " + context.getAttribute("key1")); // 读取数据 System.out.println("ContextServlet2 读取数据: " + context.getAttribute("key1")); // 读取数据 System.out.println("ContextServlet2 读取数据: " + context.getAttribute("key1")); // 读取数据 }}

在 web.xml 配置如下:

ContextServlet2 com.test01.ContextServlet2 ContextServlet2 /context2

5.重新部署 tomcat 服务,尝试首先请求 ContextServlet2 能否读取到值

也就是说,如果想要读取数据,首先需要设置数据。

6.访问 ContextServlet1 设置数据,然后再 ContextServlet2 读取数据

获得文件mime类型(文件-)

对于这个用法了解一下就好,下面我们快速用代码示例一下。

获取文件mime类型的方法:

getServletContext().getMimeType(String file)

1.读取 ​​a.mp3​​​ 和 ​​b.png​​ 的 文件mime类型

不需要实际文件,根据字符串的后缀,可以直接识别对应的类型。测试如下:

@WebServlet("/demo4")public class ServletDemo4 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //根据文件名获得文件的mini类型 //1.获得ServletContext //2.调用getMimeType()方法 String file01 = "a.mp3"; String file02 = "b.png"; String mimeType01 = getServletContext().getMimeType(file01); String mimeType02 = getServletContext().getMimeType(file02); resp.getWriter().print("ServletDemo4... file01 mimetype: " + mimeType01 + ", file02 mimetype: " + mimeType02 ); }}

获取web资源路径  ,可以将Web资源转换成字节输入流(掌握)

我们在项目工程中获取文件的资源一般操作如下:

API

String  getRealPath(String path);根据资源名称得到资源的绝对路径.getResourceAsStream(String path) ;返回制定路径文件的流

“ 注意: filepath:直接从项目的根目录开始写 ”

在web项目中,将文件转换成流,有两种方式

如果文件在resources里面,使用类加载器

InputStream is = ServletDemo04.class.getClassLoader().getResourceAsStream("文件路径");

如果文件在web里面,使用ServletContext获取文件的路径后,再将其转为文件输入流。

InputStream resourceAsStream = servletContext.getResourceAsStream("1.jpeg");

1.拷贝一张图片在 webapp 目录下,并使用 ServletContext 获取编译后的存储路径

@WebServlet("/demo5")public class ServletDemo5 extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1. 获取 图片 编译后的 存储路径 ServletContext servletContext = getServletContext(); String realPath = servletContext.getRealPath("1.jpeg"); System.out.println(realPath); }}

2.使用文件输入流读取文件内容

@WebServlet("/demo5")public class ServletDemo5 extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1. 获取 图片 编译后的 存储路径 ServletContext servletContext = getServletContext(); String realPath = servletContext.getRealPath("1.jpeg"); System.out.println(realPath); // 2. 使用文件输入流读取文件内容 FileInputStream fileInputStream = new FileInputStream(realPath); System.out.println(fileInputStream); }}

可以看到,通过这种方式已经成功获取到了 webapp 目录下的图片资源了。但是还可以直接通过 ServletContext 对象获取文件资源。

3.使用ServletContext可以获取web里面的资源的真实路径

@WebServlet("/demo5")public class ServletDemo5 extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1. 获取 图片 编译后的 存储路径 ServletContext servletContext = getServletContext(); String realPath = servletContext.getRealPath("1.jpeg"); System.out.println(realPath); // 2. 使用文件输入流读取文件内容 FileInputStream fileInputStream = new FileInputStream(realPath); System.out.println("fileInputStream: " + fileInputStream); // 3. 使用ServletContext可以获取web里面的资源的真实路径 InputStream resourceAsStream = servletContext.getResourceAsStream("1.jpeg"); System.out.println("resourceAsStream: " + resourceAsStream); }}

小结

作为域对象存取数据【共享】

setAttribute(String name,Object value)  存getAttribute(String name)  取removeAttribute(String name) 移除

获得文件的Mime类型

getMineType(String fileName);

获得全局初始化参数

在web.xml配置getInitParameter(String name);

获得web资源路径【已经在web目录了】

getRealPath(String file);   获得文件的绝对路径getReSourceAsStream(String file);   获得文件流

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:4. Tomcat 服务器部署WEB项目
下一篇:16. Servlet入门 - request介绍以及使用
相关文章

 发表评论

暂时没有评论,来抢沙发吧~