ServletContext对象

Posted 风萧萧路漫漫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ServletContext对象相关的知识,希望对你有一定的参考价值。

1.概念:代表整个web应用(整个web工程),可以和程序的容器(服务器,例如:Tomcat)通信

2.获取

     1.通过request对象获取:request.getServletContext();

     2.通过HttpServlet获取:this.getServletContext();

package cn.itcast.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 旗木五五开
 * @create 2020-03-08 8:48
 */
@WebServlet("/servletContextDemo1")
public class ServletContextDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        servletContext获取
        ServletContext context1 = request.getServletContext();
        ServletContext context2 = this.getServletContext();
        System.out.println(context1==context2);//true
        System.out.println(context1);
        System.out.println(context2);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

 

3.功能:

    1.获取MIE类型:在互联网通信过程中,定义的一种文件数据类型。(http协议也遵循这个类型的标准)

          格式:大类型/小类型  

                例如:text/html:text表示纯文本,html表示内部是HTML形式的。html形式的纯文本

                       image/jpeg:图片/jpeg类型。jpeg类型的图片

          用途:http协议响应头里有Content-Type: text/html;charset=UTF-8  设置文件的MIE类型,服务器告诉浏览器,响应体的格式,浏览器用相应的解析引擎去解析

          获取:String getMimeType(String file);接收文件的名称(后缀名)返回一个string类型的值。

              为什么能获取到,整个mime类型所有的对应关系都在服务器存储着,这个对象刚好可以服务器通信,所以可以获取到

package cn.itcast.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 旗木五五开
 * @create 2020-03-08 9:30
 */
@WebServlet("/servletContextDemo2")
public class ServletContextDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        1.通过 HttpServlet 获取
        ServletContext context = this.getServletContext();
//        2.定义文件名称。以后是通过对应的文件动态的获取
        String filename="a.jpg";
//        3.获取mime类型
        String mimeType = context.getMimeType(filename);
        System.out.println(mimeType);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

 

 

     2.域对象:共享数据

            1.设置数的名称和值:setAttribute(String name,Object value);

            2.通过名称获取对应的值:getAttribute(String name);

            3.通过名称删除对应的值:removeAttribute(String name);

          ServletContext对象范围:所有用户所有请求的数据 

          使用:要谨慎,所有用户都可以操作,不安全。而且对象的生命周期长,服务器启动就被创建了,服务器关了才能被销毁。对象驻留在内存能保持很久的时间。存储的数据多了会对内存产生压力

package cn.itcast.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 旗木五五开
 * @create 2020-03-08 9:45
 */
@WebServlet("/servletContextDemo3")
public class ServletContextDemo3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        1.通过 HttpServlet 获取
        ServletContext context = this.getServletContext();
//        2.设置数据
        context.setAttribute("msg","haha");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

 

package cn.itcast.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 旗木五五开
 * @create 2020-03-08 9:47
 */
@WebServlet("/servletContextDemo4")
public class ServletContextDemo4 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        1.通过 HttpServlet 获取
        ServletContext context = this.getServletContext();
//        2.获取数据
        Object msg = context.getAttribute("msg");
        System.out.println(msg);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

 

 

 

 

     3.获取文件的真实(服务器)路径

        

        方法:String getRealPath(String path);   String path=/资源名.后缀

package cn.itcast.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.File;
import java.io.IOException;

/**
 * @author 旗木五五开
 * @create 2020-03-09 0:58
 */
@WebServlet("/servletContextDemo5")
public class ServletContextDemo5 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        1.通过HttpServlet获取
        ServletContext context = this.getServletContext();
//        2.获取文件的服务器路径
        String b = context.getRealPath("/b.txt");//web目录下资源访问
        System.out.println(b);

        String c = context.getRealPath("/WEB-INF/c.txt");//web-inf下的资源访问
        System.out.println(c);

        String a = context.getRealPath("/WEB-INF/classes/a.txt");//src目录下的资源访问
        System.out.println(a);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

 

 

 

 

 

 

 

此目录就是:web目录下资源访问的路径。

有了路径就可以找到文件对应的目录找到后,就可以通过流,加载它进内存。

ClassLoader 类加载器:只能获取src下的目录不能获取web下的真实目录

4.特点:

      1.有两种获取方式,获取的都是同一个对象。this.getServletContext();(用这个方便)

 

以上是关于ServletContext对象的主要内容,如果未能解决你的问题,请参考以下文章

JAVA语言中用于投票器的代码如何写

servlet开发之ServletContext

在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法

三:ServletContext对象

ServletContext对象

在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法