【JavaWeb】ServletContext详解

基本概念

ServletContext,Servlet上下文,提供对应用程序中所有Servlet所共有的各种资源和功能的访问,是一个全局的储存信息的空间,一个web应用对应一个ServletContext,所以ServletContext的作用范围是整个应用,服务器开始,其就存在,服务器关闭,其才释放。为了节省空间,提高效率,ServletContext中,要放必须的、重要的、所有用户需要共享的线程又是安全的一些信息。 

ServletContext的获取方式:

1. 通过request对象获取

request.getServletContext();

2. 通过HttpServlet获取

this.getServletContext();

两种方式获取到的对象时一样的: 

package cn.lwl.web.servletcontext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**获取方式
 * @author liwenlong
 * @data 2020/3/23
 */
@WebServlet("/servletContext1")
public class ServletContextDemo1 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext sc1 = req.getServletContext();
        ServletContext sc2 = this.getServletContext();
        //输出结果为true
        System.out.println(sc1 == sc2);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}

主要的功能

  1. 获取MIME类型。
  2. 共享对象
  3. 获取文件的真实路径(服务器路径)

MIME多用途互联网邮件扩展类型。是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动使用指定应用程序来打开。举例:

 获取MIME类型

String getMimeType(String file);
package cn.lwl.web.servletcontext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**功能介绍
 *
 * 获取MIME类型
 * 共享对象
 * 获取文件的真实路径(服务器路径)
 *
 * @author liwenlong
 * @data 2020/3/23
 */
@WebServlet("/servletContext2")
public class ServletContextDemo2 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取ServletContext对象
        ServletContext sc = req.getServletContext();
        //定义文件名称
        String img = "a.png";
        //获取文件类型
        String type = sc.getMimeType(img);
        System.out.println(type);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}

 共享数据

setAttribute(String name,Object value);
getAttribute(String name);
removeAttribute(String name);

ServletContext对象的范围:可以共享所有用户的数据。

在ServletContextDemo1中设置那么为 “zhangsan” .

package cn.lwl.web.servletcontext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**获取方式
 * @author liwenlong
 * @data 2020/3/23
 */
@WebServlet("/servletContext1")
public class ServletContextDemo1 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext sc1 = req.getServletContext();
        sc1.setAttribute("name","zhangsan");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}

 在ServletContextDemo2中读取 “name”的值 .

package cn.lwl.web.servletcontext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**功能介绍
 *
 * 获取MIME类型
 * 共享对象
 * 获取文件的真实路径(服务器路径)
 *
 * @author liwenlong
 * @data 2020/3/23
 */
@WebServlet("/servletContext2")
public class ServletContextDemo2 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取ServletContext对象
        ServletContext sc = req.getServletContext();

        String name = (String) sc.getAttribute("name");
        System.out.println(name);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}

 获取文件的真实路径

获取a和c两个文件的真实路径: 

package cn.lwl.web.servletcontext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**获取方式
 * @author liwenlong
 * @data 2020/3/23
 */
@WebServlet("/servletContext3")
public class ServletContextDemo3 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext sc = req.getServletContext();
        String realPath = sc.getRealPath("/a.txt");
        System.out.println(realPath);
        String realPath2 = sc.getRealPath("/web/c.txt");
        System.out.println(realPath2);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}

输出:

发布了171 篇原创文章 · 获赞 118 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/zzu_seu/article/details/105046188