码迷,mamicode.com
首页 > 其他好文 > 详细

Servlet(五)----ServletContext对象

时间:2020-03-22 23:53:36      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:==   过程   数据类型   数据   名称   res   request对象   eal   tde   

##  ServletContext对象

1、概念:代表整个web应用,可以和程序的容器(服务器)来通信

 

2、获取:

  1、通过request对象获取

    request.getServletContext();

  2、通过HttpServlet对象获取

    this.getServletContext();

3、功能:

  1、获取MIME类型:

    *  MIME类型:在互联网通信过程中定义的一种文件数据类型

      *  格式:大类型/小类型      如:text/html             image/jpeg

    *  获取:String  getMimeType(String  file)

package com.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("/servletContextDemo01")
public class ServletContextDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、通过request对象获取
        ServletContext context1 = req.getServletContext();
        //2、通过this获取
        ServletContext context2 = this.getServletContext();
        System.out.println(context1 == context2); //true

        //3、定义文件名称
        String filename = "a.jpg";
        //4、获取mime类型
        String mimeType = context1.getMimeType(filename);
        System.out.println(mimeType);//image/jpeg
    }

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

  2、域对象:共享数据

    1、setAttribute(String  name,  Object  value)

    2、getAttribute(String  name)

    3、removeAttribute(String  name)

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

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

    1、方法:String  getRealPath(String  path)

package com.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;

@WebServlet("/servletContextDemo04")
public class ServletContextDemo04 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、通过request对象获取
        ServletContext context = req.getServletContext();
        //获取文件的服务器路径
        //web目录下资源访问
        String realPath = context.getRealPath("/a.txt");
        System.out.println(realPath);//D:\IntelliJ IDEA 2019.3.1\workspace\Web\out\artifacts\Web_war_exploded\a.txt
        //WEB-INF目录下的资源访问
        String c = context.getRealPath("/WEB-INF/c.txt");
        //src目录下资源访问
        String d = context.getRealPath("/WEB-INF/classes/c.txt");
    }

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

 

Servlet(五)----ServletContext对象

标签:==   过程   数据类型   数据   名称   res   request对象   eal   tde   

原文地址:https://www.cnblogs.com/21seu-ftj/p/12549391.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!