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

ServletContext对象

时间:2020-03-08 10:08:03      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:auth   charset   服务   res   move   动态   har   互联网通信   row   

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.获取文件的真实(服务器)路径

        

 

4.特点:

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

 

ServletContext对象

标签:auth   charset   服务   res   move   动态   har   互联网通信   row   

原文地址:https://www.cnblogs.com/rijiyuelei/p/12440943.html

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