ServletContext对象_hehe.employment.over.15.6

文章目录

15.9 ServletContext对象

ServletContext对象: 代表整个web应用,可以和程序的容器(服务器)来通信。

15.9.1 获取

  • 通过request对象获取: request.getServletContext();
  • 通过HttpServlet获取: this.getServletContext();
  • 示例:
package com.xww.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;

@WebServlet("/servletContextDemo1")
public class ServletContextDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 通过request对象获取
        ServletContext context1 = request.getServletContext();
        //2. 通过HttpServlet获取
        ServletContext context2 = this.getServletContext();

        System.out.println(context1);
        System.out.println(context2);
        System.out.println(context1 == context2);//true
    }

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

15.9.2 功能

  • 1.获取MIME类型:
    • MIME类型: 在互联网通信过程中定义的一种文件数据类型
    • 格式: 大类型/小类型 eg: text/html
    • 获取: String getMimeType(String file)
    • 示例:
  • 2.域对象:共享数据
    • setAttribute(String name,Object value)
    • getAttribute(String name)
    • removeAttribute(String name)
    • ServletContext对象范围: 所有用户所有请求的数据。
  • 3. 获取文件的真实(服务器)路径
    • 方法: String getRealPath(String path)
  • 示例:
package com.xww.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;

@WebServlet("/servletContextDemo2")
public class ServletContextDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //2. 通过HttpServlet获取
        ServletContext context = this.getServletContext();
        //3. 定义文件名称
        String filename = "a.jpg";
        //4.获取MIME类型
        String mimeType = context.getMimeType(filename);
        System.out.println(mimeType);//image/jpeg

        //设置数据
        context.setAttribute("name","xww");

        // 获取文件的服务器路径
        String b = context.getRealPath("/b.txt");//web目录下资源访问
        System.out.println(b);
        //D:\项目_间接\idea_间接\code_employment_two\out\artifacts\day15_response_war_exploded\b.txt

        String c = context.getRealPath("/WEB-INF/c.txt");//WEB-INF目录下的资源访问
        System.out.println(c);
        //D:\项目_间接\idea_间接\code_employment_two\out\artifacts\day15_response_war_exploded\WEB-INF\c.txt

        String a = context.getRealPath("/WEB-INF/classes/a.txt");//src目录下的资源访问,src目录中的内容会被放到/WEB-INF/classes目录中
        System.out.println(a);
        //D:\项目_间接\idea_间接\code_employment_two\out\artifacts\day15_response_war_exploded\WEB-INF\classes\a.txt

    }

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

上一篇:Response_案例_验证码_hehe.employment.over.15.5


下一篇:JavaWeb-Servlet